Skip to content

Commit

Permalink
Update tests to use test case format
Browse files Browse the repository at this point in the history
  • Loading branch information
rharpavat authored and bgavrilMS committed Feb 16, 2024
1 parent 76baf60 commit 9a3337c
Showing 1 changed file with 96 additions and 31 deletions.
127 changes: 96 additions & 31 deletions apps/public/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,46 +859,111 @@ func TestWithDomainHint(t *testing.T) {
func TestWithAuthenticationScheme(t *testing.T) {
clientInfo := base64.RawStdEncoding.EncodeToString([]byte(`{"uid":"uid","utid":"utid"}`))
lmo, tenant := "login.microsoftonline.com", "tenant"
authori := fmt.Sprintf(authorityFmt, lmo, tenant)
accessToken, idToken, refreshToken := "at", mock.GetIDToken(tenant, lmo), "rt"
authScheme := mock.NewTestAuthnScheme()

mockClient := mock.Client{}
mockClient.AppendResponse(mock.WithBody(mock.GetTenantDiscoveryBody(lmo, tenant)))
mockClient.AppendResponse(mock.WithBody(mock.GetAccessTokenBody(accessToken, idToken, refreshToken, clientInfo, 3600)))
client, err := New("client-id", WithAuthority(authori), WithHTTPClient(&mockClient))
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
var ar AuthResult
ar, err = client.AcquireTokenInteractive(ctx, tokenScope, WithAuthenticationScheme(authScheme), WithOpenURL(fakeBrowserOpenURL))
var client Client
var err error

if err != nil {
t.Fatal(err)
}
if ar.AccessToken != fmt.Sprintf(mock.Authnschemeformat, accessToken) {
t.Fatalf(`unexpected access token "%s"`, ar.AccessToken)
for _, testCase := range []struct {
name string
responses [][]byte
}{
{
name: "interactive",
responses: [][]byte{
mock.GetTenantDiscoveryBody(lmo, tenant),
mock.GetAccessTokenBody(accessToken, idToken, refreshToken, clientInfo, 3600),
},
},
{
name: "password",
responses: [][]byte{
mock.GetTenantDiscoveryBody(lmo, tenant),
[]byte(`{"account_type":"Managed","cloud_audience_urn":"urn","cloud_instance_name":"...","domain_name":"..."}`),
mock.GetAccessTokenBody(accessToken, idToken, refreshToken, clientInfo, 3600),
},
},
} {
t.Run(testCase.name, func(t *testing.T) {
ctx := context.Background()

// get a fresh client to avoid any overflow from other tests
client, err = getNewClientWithMockedResponses(
testCase.responses,
lmo,
tenant,
accessToken,
idToken,
refreshToken,
clientInfo,
true,
)
if err != nil {
t.Fatal(err)
}

// first, run the test case and try to acquire a token with the mock flow
switch testCase.name {
case "interactive":
ar, err = client.AcquireTokenInteractive(ctx, tokenScope, WithAuthenticationScheme(authScheme), WithOpenURL(fakeBrowserOpenURL))
case "password":
ar, err = client.AcquireTokenByUsernamePassword(ctx, tokenScope, "username", "password", WithAuthenticationScheme(authScheme))
default:
t.Fatalf("test bug: no test for " + testCase.name)
}

// validate that the token is created correctly
if err != nil {
t.Fatal(err)
}

// and that the token is formatted according to the authentication scheme
if ar.AccessToken != fmt.Sprintf(mock.Authnschemeformat, accessToken) {
t.Fatalf(`unexpected access token "%s"`, ar.AccessToken)
}

// next, try acquiring the token again silently from the cache, and validate the token again
ar, err = client.AcquireTokenSilent(ctx, tokenScope, WithSilentAccount(ar.Account), WithAuthenticationScheme(authScheme))
if err != nil {
t.Fatal(err)
}

if ar.AccessToken != fmt.Sprintf(mock.Authnschemeformat, accessToken) {
t.Fatalf(`unexpected access token "%s"`, ar.AccessToken)
}
})
}
mockClient.AppendResponse(mock.WithBody(mock.GetInstanceDiscoveryBody(lmo, tenant)))
mockClient.AppendResponse(mock.WithBody(mock.GetAccessTokenBody(accessToken, idToken, refreshToken, clientInfo, 3600)))
}

ar, err = client.AcquireTokenSilent(ctx, tokenScope, WithSilentAccount(ar.Account), WithAuthenticationScheme(authScheme))
if err != nil {
t.Fatal(err)
// gets a new public.Client instance with the given responses pre-added to the underlying mock HttpClient
func getNewClientWithMockedResponses(
responses [][]byte,
lmo,
tenant,
accessToken,
idToken,
refreshToken,
clientInfo string,
includeAcquireSilentResponses bool,
) (Client, error) {
authority := fmt.Sprintf(authorityFmt, lmo, tenant)

mockClient := mock.Client{}
for _, response := range responses {
mockClient.AppendResponse(mock.WithBody(response))
}
if ar.AccessToken != fmt.Sprintf(mock.Authnschemeformat, accessToken) {
t.Fatalf(`unexpected access token "%s"`, ar.AccessToken)

if includeAcquireSilentResponses {
// we will be testing the AcquireTokenSilent flow after the initial flow, so append the correct responses
mockClient.AppendResponse(mock.WithBody(mock.GetInstanceDiscoveryBody(lmo, tenant)))
mockClient.AppendResponse(mock.WithBody(mock.GetAccessTokenBody(accessToken, idToken, refreshToken, clientInfo, 3600)))
}

mockClient.AppendResponse(mock.WithBody(mock.GetTenantDiscoveryBody(lmo, tenant)))
mockClient.AppendResponse(mock.WithBody([]byte(`{"account_type":"Managed","cloud_audience_urn":"urn","cloud_instance_name":"...","domain_name":"..."}`)))
mockClient.AppendResponse(mock.WithBody(mock.GetAccessTokenBody(accessToken, idToken, refreshToken, clientInfo, 3600)))
ar, err = client.AcquireTokenByUsernamePassword(ctx, tokenScope, "username", "password", WithAuthenticationScheme(authScheme))
client, err := New("client-id", WithAuthority(authority), WithHTTPClient(&mockClient))
if err != nil {
t.Fatal(err)
}
if ar.AccessToken != fmt.Sprintf(mock.Authnschemeformat, accessToken) {
t.Fatalf(`unexpected access token "%s"`, ar.AccessToken)
return client, fmt.Errorf("failed to create new public client with error: %w", err)
}

return client, nil
}

0 comments on commit 9a3337c

Please sign in to comment.