Skip to content

Commit

Permalink
add unit tests for validating emailToLowercase behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
hur committed Aug 16, 2024
1 parent 66b68d8 commit 379d639
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions connector/microsoft/microsoft_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package microsoft

import (
"context"
"encoding/json"
"fmt"
"net/http"
Expand Down Expand Up @@ -101,6 +102,67 @@ func TestUserIdentityFromGraphAPI(t *testing.T) {
expectEquals(t, len(identity.Groups), 0)
}

func TestHandleCallbackWithEmailToLowercase(t *testing.T) {
s := newTestServer(map[string]testResponse{
"/v1.0/me?$select=id,displayName,userPrincipalName": {
data: user{ID: "S56767889", Name: "Jane Doe", Email: "[email protected]"},
},
"/" + tenant + "/oauth2/v2.0/token": dummyToken,
})
defer s.Close()

req, _ := http.NewRequest("GET", s.URL, nil)

c := microsoftConnector{apiURL: s.URL, graphURL: s.URL, tenant: tenant, emailToLowercase: true}
identity, err := c.HandleCallback(connector.Scopes{Groups: false}, req)
expectNil(t, err)
expectEquals(t, identity.Username, "Jane Doe")
expectEquals(t, identity.UserID, "S56767889")
expectEquals(t, identity.PreferredUsername, "")
expectEquals(t, identity.Email, "[email protected]")
expectEquals(t, identity.EmailVerified, true)
expectEquals(t, len(identity.Groups), 0)
}

func TestRefreshWithEmailToLowercase(t *testing.T) {
s := newTestServer(map[string]testResponse{
"/v1.0/me?$select=id,displayName,userPrincipalName": {
data: user{ID: "S56767889", Name: "Jane Doe", Email: "[email protected]"},
},
"/" + tenant + "/oauth2/v2.0/token": {data: map[string]interface{}{
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9",
"refresh_token": "oRzxVjCnohYRHEYEhZshkmakKmoyVoTjfUGC",
"expires_in": "30",
}},
})
defer s.Close()

req, err := http.NewRequest("GET", s.URL, nil)
expectNil(t, err)

c := microsoftConnector{apiURL: s.URL, graphURL: s.URL, tenant: tenant, emailToLowercase: true}

expectNil(t, err)

identity, err := c.HandleCallback(connector.Scopes{Groups: false, OfflineAccess: true}, req)
expectNil(t, err)
expectEquals(t, identity.Username, "Jane Doe")
expectEquals(t, identity.UserID, "S56767889")
expectEquals(t, identity.PreferredUsername, "")
expectEquals(t, identity.Email, "[email protected]")
expectEquals(t, identity.EmailVerified, true)
expectEquals(t, len(identity.Groups), 0)

identity, err = c.Refresh(context.Background(), connector.Scopes{Groups: false, OfflineAccess: true}, identity)
expectNil(t, err)
expectEquals(t, identity.Username, "Jane Doe")
expectEquals(t, identity.UserID, "S56767889")
expectEquals(t, identity.PreferredUsername, "")
expectEquals(t, identity.Email, "[email protected]")
expectEquals(t, identity.EmailVerified, true)
expectEquals(t, len(identity.Groups), 0)
}

func TestUserGroupsFromGraphAPI(t *testing.T) {
s := newTestServer(map[string]testResponse{
"/v1.0/me?$select=id,displayName,userPrincipalName": {data: user{}},
Expand Down

0 comments on commit 379d639

Please sign in to comment.