Skip to content

Commit

Permalink
Updated Testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
developerkunal committed Sep 23, 2024
1 parent 64375d3 commit 3d1c5b9
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions authentication/authentication_error_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package authentication

import (
"errors"
"io"
"net/http"
"strings"
Expand All @@ -11,9 +12,10 @@ import (

func Test_newError(t *testing.T) {
var testCases = []struct {
name string
givenResponse http.Response
expectedError Error
name string
givenResponse http.Response
expectedError Error
expectedMFAToken string
}{
{
name: "it fails to decode if body is not json",
Expand All @@ -26,6 +28,7 @@ func Test_newError(t *testing.T) {
Err: "Forbidden",
Message: "failed to decode json error response payload: invalid character 'H' looking for beginning of value",
},
expectedMFAToken: "",
},
{
name: "it correctly decodes the error response payload",
Expand All @@ -38,6 +41,7 @@ func Test_newError(t *testing.T) {
Err: "invalid_scope",
Message: "Scope must be an array or a string",
},
expectedMFAToken: "",
},
{
name: "it will still post the correct status code if the body doesn't have the correct structure",
Expand All @@ -50,6 +54,7 @@ func Test_newError(t *testing.T) {
Err: "Internal Server Error",
Message: "",
},
expectedMFAToken: "",
},
{
name: "it will handle an invalid sign up response",
Expand All @@ -62,6 +67,7 @@ func Test_newError(t *testing.T) {
Err: "invalid_signup",
Message: "Invalid sign up",
},
expectedMFAToken: "",
},
{
name: "it will handle invalid password response",
Expand All @@ -74,13 +80,32 @@ func Test_newError(t *testing.T) {
Err: "invalid_password",
Message: `{"rules":[{"message":"At least %d characters in length","format":[8],"code":"lengthAtLeast","verified":true},{"message":"Contain at least %d of the following %d types of characters:","code":"containsAtLeast","format":[3,4],"items":[{"message":"lower case letters (a-z)","code":"lowerCase","verified":true},{"message":"upper case letters (A-Z)","code":"upperCase","verified":false},{"message":"numbers (i.e. 0-9)","code":"numbers","verified":false},{"message":"special characters (e.g. !@#$%^&*)","code":"specialCharacters","verified":true}],"verified":false}],"verified":false}`,
},
expectedMFAToken: "",
},
{
name: "it will handle invalid password response with MFA token",
givenResponse: http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser((strings.NewReader(`{"name":"PasswordStrengthError","message":"Password is too weak","code":"invalid_password","description":{"rules":[{"message":"At least %d characters in length","format":[8],"code":"lengthAtLeast","verified":true},{"message":"Contain at least %d of the following %d types of characters:","code":"containsAtLeast","format":[3,4],"items":[{"message":"lower case letters (a-z)","code":"lowerCase","verified":true},{"message":"upper case letters (A-Z)","code":"upperCase","verified":false},{"message":"numbers (i.e. 0-9)","code":"numbers","verified":false},{"message":"special characters (e.g. !@#$%^&*)","code":"specialCharacters","verified":true}],"verified":false}],"verified":false},"policy":"* At least 8 characters in length\n* Contain at least 3 of the following 4 types of characters:\n * lower case letters (a-z)\n * upper case letters (A-Z)\n * numbers (i.e. 0-9)\n * special characters (e.g. !@#$%^&*)","mfa_token":"123456","statusCode":400}`))),
},
expectedError: Error{
StatusCode: 400,
Err: "invalid_password",
Message: `{"rules":[{"message":"At least %d characters in length","format":[8],"code":"lengthAtLeast","verified":true},{"message":"Contain at least %d of the following %d types of characters:","code":"containsAtLeast","format":[3,4],"items":[{"message":"lower case letters (a-z)","code":"lowerCase","verified":true},{"message":"upper case letters (A-Z)","code":"upperCase","verified":false},{"message":"numbers (i.e. 0-9)","code":"numbers","verified":false},{"message":"special characters (e.g. !@#$%^&*)","code":"specialCharacters","verified":true}],"verified":false}],"verified":false}`,
MFAToken: "123456",
},
expectedMFAToken: "123456",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
actualError := newError(&testCase.givenResponse)
assert.Equal(t, &testCase.expectedError, actualError)
err := newError(&testCase.givenResponse)
var actualError *Error
ok := errors.As(err, &actualError)
assert.True(t, ok, "newError should return an *Error")
assert.Equal(t, testCase.expectedError, *actualError)
assert.Equal(t, testCase.expectedMFAToken, actualError.GetMFAToken())
})
}
}

0 comments on commit 3d1c5b9

Please sign in to comment.