Skip to content

Commit

Permalink
test: add admin otp handler test
Browse files Browse the repository at this point in the history
  • Loading branch information
FreddyDevelop committed Nov 15, 2024
1 parent e019334 commit 5f23c7e
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 0 deletions.
145 changes: 145 additions & 0 deletions backend/handler/otp_admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package handler

import (
"encoding/json"
"fmt"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/suite"
"github.com/teamhanko/hanko/backend/dto/admin"
"github.com/teamhanko/hanko/backend/test"
"net/http"
"net/http/httptest"
"testing"
)

func TestOtpAdminSuite(t *testing.T) {
t.Parallel()
suite.Run(t, new(otpAdminSuite))
}

type otpAdminSuite struct {
test.Suite
}

func (s *otpAdminSuite) TestOtpAdminHandler_Get() {
if testing.Short() {
s.T().Skip("skipping test in short mode.")
}

err := s.LoadFixtures("../test/fixtures/otp")
s.Require().NoError(err)

e := NewAdminRouter(&test.DefaultConfig, s.Storage, nil)

tests := []struct {
name string
userId string
expectedStatusCode int
}{
{
name: "should return otp credential",
userId: "b5dd5267-b462-48be-b70d-bcd6f1bbe7a5",
expectedStatusCode: http.StatusOK,
},
{
name: "should fail if user has no otp credential",
userId: "38bf5a00-d7ea-40a5-a5de-48722c148925",
expectedStatusCode: http.StatusNotFound,
},
{
name: "should fail on non uuid userID",
userId: "customUserId",
expectedStatusCode: http.StatusBadRequest,
},
{
name: "should fail on empty userID",
userId: "",
expectedStatusCode: http.StatusBadRequest,
},
{
name: "should fail on non existing user",
userId: "30f41697-b413-43cc-8cca-d55298683607",
expectedStatusCode: http.StatusNotFound,
},
}

for _, currentTest := range tests {
s.Run(currentTest.name, func() {
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("/users/%s/otp", currentTest.userId), nil)

rec := httptest.NewRecorder()

e.ServeHTTP(rec, req)

if http.StatusOK == rec.Code {
var otpCredential *admin.OTPDto
s.NoError(json.Unmarshal(rec.Body.Bytes(), &otpCredential))
s.NotNil(otpCredential)
} else {
s.Require().Equal(currentTest.expectedStatusCode, rec.Code)
}
})
}
}

func (s *otpAdminSuite) TestOtpAdminHandler_Delete() {
if testing.Short() {
s.T().Skip("skipping test in short mode.")
}

err := s.LoadFixtures("../test/fixtures/otp")
s.Require().NoError(err)

e := NewAdminRouter(&test.DefaultConfig, s.Storage, nil)

tests := []struct {
name string
userId string
expectedStatusCode int
}{
{
name: "should delete the otp credential",
userId: "b5dd5267-b462-48be-b70d-bcd6f1bbe7a5",
expectedStatusCode: http.StatusNoContent,
},
{
name: "should fail if user has no otp credential",
userId: "38bf5a00-d7ea-40a5-a5de-48722c148925",
expectedStatusCode: http.StatusNotFound,
},
{
name: "should fail on non uuid userID",
userId: "customUserId",
expectedStatusCode: http.StatusBadRequest,
},
{
name: "should fail on empty userID",
userId: "",
expectedStatusCode: http.StatusBadRequest,
},
{
name: "should fail on non existing user",
userId: "30f41697-b413-43cc-8cca-d55298683607",
expectedStatusCode: http.StatusNotFound,
},
}

for _, currentTest := range tests {
s.Run(currentTest.name, func() {
req := httptest.NewRequest(http.MethodDelete, fmt.Sprintf("/users/%s/otp", currentTest.userId), nil)
req.Header.Set("Content-Type", "application/json")

rec := httptest.NewRecorder()

e.ServeHTTP(rec, req)

s.Require().Equal(currentTest.expectedStatusCode, rec.Code)

if http.StatusNoContent == rec.Code {
cred, err := s.Storage.GetPasswordCredentialPersister().GetByUserID(uuid.FromStringOrNil(currentTest.userId))
s.Require().NoError(err)
s.Require().Nil(cred)
}
})
}
}
36 changes: 36 additions & 0 deletions backend/test/fixtures/otp/emails.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
- id: 51b7c175-ceb6-45ba-aae6-0092221c1b84
user_id: b5dd5267-b462-48be-b70d-bcd6f1bbe7a5
address: [email protected]
verified: true
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
- id: 38bf5a00-d7ea-40a5-a5de-48722c148925
user_id: 38bf5a00-d7ea-40a5-a5de-48722c148925
address: [email protected]
verified: true
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
- id: e0282f3f-b211-4f0e-b777-6fabc69287c9
user_id: e0282f3f-b211-4f0e-b777-6fabc69287c9
address: [email protected]
verified: true
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
- id: 8bb4c8a7-a3e6-48bb-b54f-20e3b485ab33
user_id: b5dd5267-b462-48be-b70d-bcd6f1bbe7a5
address: [email protected]
verified: true
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
- id: f194ee0f-dd1a-48f7-8766-c67e4d6cd1fe
user_id: b5dd5267-b462-48be-b70d-bcd6f1bbe7a5
address: [email protected]
verified: true
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
- id: 0394ad95-6ea3-4cbd-940b-3f6dd10440b0
user_id: 38bf5a00-d7ea-40a5-a5de-48722c148925
address: [email protected]
verified: true
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
5 changes: 5 additions & 0 deletions backend/test/fixtures/otp/otp_secrets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- id: f28b15df-6e09-4ac0-b49f-e4e2d274f939
user_id: b5dd5267-b462-48be-b70d-bcd6f1bbe7a5
secret: RANDOMOTPSECRET
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
10 changes: 10 additions & 0 deletions backend/test/fixtures/otp/primary_emails.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- id: 8fe72e5f-edb6-40e7-83a7-a7e858c2c62d
email_id: 51b7c175-ceb6-45ba-aae6-0092221c1b84
user_id: b5dd5267-b462-48be-b70d-bcd6f1bbe7a5
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
- id: 3a5f340f-07f7-40dc-a507-d5919915e11d
email_id: 38bf5a00-d7ea-40a5-a5de-48722c148925
user_id: 38bf5a00-d7ea-40a5-a5de-48722c148925
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
13 changes: 13 additions & 0 deletions backend/test/fixtures/otp/users.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
- id: b5dd5267-b462-48be-b70d-bcd6f1bbe7a5
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
- id: 38bf5a00-d7ea-40a5-a5de-48722c148925
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
- id: e0282f3f-b211-4f0e-b777-6fabc69287c9
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59
- id: d41df4b7-c055-45e6-9faf-61aa92a4032e
created_at: 2020-12-31 23:59:59
updated_at: 2020-12-31 23:59:59

0 comments on commit 5f23c7e

Please sign in to comment.