Skip to content

Commit

Permalink
Tests: tweak assertAuthorizationSuccess to accept empty strings for…
Browse files Browse the repository at this point in the history
… `avatar`

Certain providers (eg. WorkOS, Azure) do not return an avatar URL in
their user profile data. Hence, it is possible for
`user.UserMetaData["avatar_url"]` to be `nil`. However, the
`assertAuthorizationSuccess` function only accepts a `string` to compare
against the stored avatar URL in a test, causing certain tests to fail
when a

This PR tweaks the test for avatar URL to check for `nil` if the
supplied expected input is `""` (the empty string). Various tests are
also modified for the Azure provider, which previously had workarounds
including already creating a user before the signup occurred, which
meant the initial login flow wasn't thoroughly tested.

The alternative would be to change the type of `avatar` to `*string`,
which is a lot more troublesome to write for in the common case of
having an avatar URL, since you can't use the `&` referencing syntax for
string constants.
  • Loading branch information
bnjmnt4n committed Feb 27, 2022
1 parent 34068bc commit 20c18c3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
11 changes: 5 additions & 6 deletions api/external_azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func AzureTestSignupSetup(ts *ExternalTestSuite, tokenCount *int, userCount *int

func (ts *ExternalTestSuite) TestSignupExternalAzure_AuthorizationCode() {
ts.Config.DisableSignup = false
ts.createUser("azuretestid", "[email protected]", "Azure Test", "", "")
tokenCount, userCount := 0, 0
code := "authcode"
server := AzureTestSignupSetup(ts, &tokenCount, &userCount, code, azureUser)
Expand Down Expand Up @@ -106,7 +105,7 @@ func (ts *ExternalTestSuite) TestSignupExternalAzureDisableSignupErrorWhenNoEmai
func (ts *ExternalTestSuite) TestSignupExternalAzureDisableSignupSuccessWithPrimaryEmail() {
ts.Config.DisableSignup = true

ts.createUser("azuretestid", "[email protected]", "Azure Test", "", "")
ts.createUser("azuretestid", "[email protected]", "Azure Test", "http://example.com/avatar", "")

tokenCount, userCount := 0, 0
code := "authcode"
Expand All @@ -115,12 +114,12 @@ func (ts *ExternalTestSuite) TestSignupExternalAzureDisableSignupSuccessWithPrim

u := performAuthorization(ts, "azure", code, "")

assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "Azure Test", "azuretestid", "")
assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "Azure Test", "azuretestid", "http://example.com/avatar")
}

func (ts *ExternalTestSuite) TestInviteTokenExternalAzureSuccessWhenMatchingToken() {
// name and avatar should be populated from Azure API
ts.createUser("azuretestid", "[email protected]", "", "", "invite_token")
// name should be populated from Azure API
ts.createUser("azuretestid", "[email protected]", "", "http://example.com/avatar", "invite_token")

tokenCount, userCount := 0, 0
code := "authcode"
Expand All @@ -129,7 +128,7 @@ func (ts *ExternalTestSuite) TestInviteTokenExternalAzureSuccessWhenMatchingToke

u := performAuthorization(ts, "azure", code, "invite_token")

assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "Azure Test", "azuretestid", "")
assertAuthorizationSuccess(ts, u, tokenCount, userCount, "[email protected]", "Azure Test", "azuretestid", "http://example.com/avatar")
}

func (ts *ExternalTestSuite) TestInviteTokenExternalAzureErrorWhenNoMatchingToken() {
Expand Down
6 changes: 5 additions & 1 deletion api/external_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ func assertAuthorizationSuccess(ts *ExternalTestSuite, u *url.URL, tokenCount in
ts.Require().NoError(err)
ts.Equal(providerId, user.UserMetaData["provider_id"])
ts.Equal(name, user.UserMetaData["full_name"])
ts.Equal(avatar, user.UserMetaData["avatar_url"])
if avatar == "" {
ts.Equal(nil, user.UserMetaData["avatar_url"])
} else {
ts.Equal(avatar, user.UserMetaData["avatar_url"])
}
}

func assertAuthorizationFailure(ts *ExternalTestSuite, u *url.URL, errorDescription string, errorType string, email string) {
Expand Down

0 comments on commit 20c18c3

Please sign in to comment.