Skip to content

Commit

Permalink
Add some missing asserts, simplify MakeUser
Browse files Browse the repository at this point in the history
  • Loading branch information
torcolvin committed Jan 18, 2024
1 parent 90b7ace commit 0215087
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
26 changes: 16 additions & 10 deletions rest/oidc_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ func TestOpenIDConnectAuthCodeFlow(t *testing.T) {
client := &http.Client{Jar: jar}
response, err := client.Do(request)
require.NoError(t, err, "Error sending request")
defer func() { assert.NoError(t, response.Body.Close()) }()
if (forceError{}) != tc.forceAuthError {
assertHttpResponse(t, response, tc.forceAuthError)
return
Expand All @@ -868,7 +869,6 @@ func TestOpenIDConnectAuthCodeFlow(t *testing.T) {
require.Equal(t, http.StatusOK, response.StatusCode)
var authResponseActual OIDCTokenResponse
require.NoError(t, err, json.NewDecoder(response.Body).Decode(&authResponseActual))
require.NoError(t, response.Body.Close(), "Error closing response body")
assert.NotEmpty(t, authResponseActual.SessionID, "session_id doesn't exist")
assert.Equal(t, "foo_noah", authResponseActual.Username, "name mismatch")

Expand All @@ -889,9 +889,9 @@ func TestOpenIDConnectAuthCodeFlow(t *testing.T) {
request.Header.Add("Authorization", BearerToken+" "+authResponseActual.IDToken)
response, err = client.Do(request)
require.NoError(t, err, "Error sending request with bearer token")
defer func() { assert.NoError(t, response.Body.Close()) }()
require.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, json.NewDecoder(response.Body).Decode(&responseBody))
require.NoError(t, response.Body.Close(), "Error closing response body")
assert.Equal(t, restTester.DatabaseConfig.Name, responseBody["db_name"])

// Refresh auth token using the refresh token received from OP.
Expand All @@ -902,6 +902,7 @@ func TestOpenIDConnectAuthCodeFlow(t *testing.T) {
require.NoError(t, err, "Error creating new request")
response, err = client.Do(request)
require.NoError(t, err, "Error sending request")
defer func() { assert.NoError(t, response.Body.Close()) }()
if (forceError{}) != tc.forceRefreshError {
assertHttpResponse(t, response, tc.forceRefreshError)
return
Expand All @@ -911,7 +912,6 @@ func TestOpenIDConnectAuthCodeFlow(t *testing.T) {
// Validate received token refresh response.
var refreshResponseActual OIDCTokenResponse
require.NoError(t, err, json.NewDecoder(response.Body).Decode(&refreshResponseActual))
require.NoError(t, response.Body.Close(), "Error closing response body")
refreshResponseExpected := mockAuthServer.options.tokenResponse
assert.NotEmpty(t, refreshResponseActual.SessionID, "session_id doesn't exist")
assert.Equal(t, "foo_noah", refreshResponseActual.Username, "name mismatch")
Expand All @@ -927,9 +927,9 @@ func TestOpenIDConnectAuthCodeFlow(t *testing.T) {
request.Header.Add("Authorization", BearerToken+" "+refreshResponseActual.IDToken)
response, err = client.Do(request)
require.NoError(t, err, "Error sending request with bearer token")
defer func() { assert.NoError(t, response.Body.Close()) }()
require.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, json.NewDecoder(response.Body).Decode(&responseBody))
require.NoError(t, response.Body.Close(), "Error closing response body")
assert.Equal(t, restTester.DatabaseConfig.Name, responseBody["db_name"])

// Make a keyspace-scoped request
Expand All @@ -938,8 +938,8 @@ func TestOpenIDConnectAuthCodeFlow(t *testing.T) {
request.Header.Add("Authorization", BearerToken+" "+refreshResponseActual.IDToken)
response, err = client.Do(request)
require.NoError(t, err, "Error sending request with bearer token")
defer func() { assert.NoError(t, response.Body.Close()) }()
require.Equal(t, http.StatusCreated, response.StatusCode)
require.NoError(t, response.Body.Close(), "Error closing response body")
})
}
}
Expand Down Expand Up @@ -1072,6 +1072,7 @@ func TestOpenIDConnectImplicitFlow(t *testing.T) {
request := createOIDCRequest(t, sessionEndpoint, token)
response, err := http.DefaultClient.Do(request)
require.NoError(t, err, "Error sending request with bearer token")
defer func() { assert.NoError(t, response.Body.Close()) }()

if (forceError{}) != tc.expectedError {
assertHttpResponse(t, response, tc.expectedError)
Expand Down Expand Up @@ -1290,6 +1291,7 @@ func TestOpenIDConnectImplicitFlowEdgeCases(t *testing.T) {

runBadAuthTest := func(claimSet claimSet) {
response, err := sendAuthRequest(claimSet)
defer func() { assert.NoError(t, response.Body.Close()) }()
require.NoError(t, err, "Error sending request with bearer token")
expectedAuthError := forceError{
expectedErrorCode: http.StatusUnauthorized,
Expand All @@ -1300,6 +1302,7 @@ func TestOpenIDConnectImplicitFlowEdgeCases(t *testing.T) {

runGoodAuthTest := func(claimSet claimSet, username string) {
response, err := sendAuthRequest(claimSet)
defer func() { assert.NoError(t, response.Body.Close()) }()
require.NoError(t, err, "Error sending request with bearer token")
checkGoodAuthResponse(t, restTester, response, username)
}
Expand Down Expand Up @@ -1930,6 +1933,7 @@ func TestCallbackStateClientCookies(t *testing.T) {
t.Run("unsuccessful auth when callback state enabled with no cookies support from client", func(t *testing.T) {
response, err := http.DefaultClient.Do(request)
require.NoError(t, err, "Error sending request")
defer func() { assert.NoError(t, response.Body.Close()) }()
expectedAuthError := forceError{
expectedErrorCode: http.StatusBadRequest,
expectedErrorMessage: ErrNoStateCookie.Message,
Expand All @@ -1943,10 +1947,10 @@ func TestCallbackStateClientCookies(t *testing.T) {
client := &http.Client{Jar: jar}
response, err := client.Do(request)
require.NoError(t, err, "Error sending request")
defer func() { assert.NoError(t, response.Body.Close()) }()
require.Equal(t, http.StatusOK, response.StatusCode)
var authResponseActual OIDCTokenResponse
require.NoError(t, err, json.NewDecoder(response.Body).Decode(&authResponseActual))
require.NoError(t, response.Body.Close(), "Error closing response body")
assert.NotEmpty(t, authResponseActual.SessionID, "session_id doesn't exist")
assert.Equal(t, "foo_noah", authResponseActual.Username, "name mismatch")
})
Expand All @@ -1955,11 +1959,11 @@ func TestCallbackStateClientCookies(t *testing.T) {
restTester.DatabaseConfig.OIDCConfig.Providers.GetDefaultProvider().DisableCallbackState = true
response, err := http.DefaultClient.Do(request)
require.NoError(t, err, "Error sending request")
defer func() { assert.NoError(t, response.Body.Close()) }()
require.Equal(t, http.StatusOK, response.StatusCode)

var authResponseActual OIDCTokenResponse
require.NoError(t, err, json.NewDecoder(response.Body).Decode(&authResponseActual))
require.NoError(t, response.Body.Close(), "Error closing response body")
assert.NotEmpty(t, authResponseActual.SessionID, "session_id doesn't exist")
assert.Equal(t, "foo_noah", authResponseActual.Username, "name mismatch")
})
Expand Down Expand Up @@ -2187,6 +2191,7 @@ func TestOpenIDConnectAuthCodeFlowWithUsernameClaim(t *testing.T) {
client := &http.Client{Jar: jar}
response, err := client.Do(request)
require.NoError(t, err, "Error sending request")
defer func() { assert.NoError(t, response.Body.Close()) }()
if (forceError{}) != tc.authErrorExpected {
assertHttpResponse(t, response, tc.authErrorExpected)
return
Expand All @@ -2195,7 +2200,6 @@ func TestOpenIDConnectAuthCodeFlowWithUsernameClaim(t *testing.T) {
require.Equal(t, http.StatusOK, response.StatusCode)
var authResponseActual OIDCTokenResponse
require.NoError(t, err, json.NewDecoder(response.Body).Decode(&authResponseActual))
require.NoError(t, response.Body.Close(), "Error closing response body")
assert.NotEmpty(t, authResponseActual.SessionID, "session_id doesn't exist")
expectedUsername := tc.usernameExpected
if strings.Contains(expectedUsername, "$issuer") {
Expand All @@ -2215,9 +2219,9 @@ func TestOpenIDConnectAuthCodeFlowWithUsernameClaim(t *testing.T) {
request.Header.Add("Authorization", BearerToken+" "+authResponseActual.IDToken)
response, err = client.Do(request)
require.NoError(t, err, "Error sending request with bearer token")
defer func() { assert.NoError(t, response.Body.Close()) }()
require.Equal(t, http.StatusOK, response.StatusCode)
require.NoError(t, json.NewDecoder(response.Body).Decode(&responseBody))
require.NoError(t, response.Body.Close(), "Error closing response body")
assert.Equal(t, restTester.DatabaseConfig.Name, responseBody["db_name"])
})
}
Expand Down Expand Up @@ -2295,13 +2299,15 @@ func TestEventuallyReachableOIDCClient(t *testing.T) {
request = createOIDCRequest(t, sessionEndpoint, token)
response, err = http.DefaultClient.Do(request)
require.NoError(t, err, "Error sending request with bearer token")
defer func() { assert.NoError(t, response.Body.Close()) }()
checkGoodAuthResponse(t, restTester, response, "foo_noah")

// Unreachable again after being reachable - still success
refreshProviderConfig(restTester.DatabaseConfig.OIDCConfig.Providers, unreachableAddr)
request = createOIDCRequest(t, sessionEndpoint, token)
response, err = http.DefaultClient.Do(request)
require.NoError(t, err, "Error sending request with bearer token")
defer func() { assert.NoError(t, response.Body.Close()) }()
checkGoodAuthResponse(t, restTester, response, "foo_noah")
})
}
Expand Down Expand Up @@ -2416,11 +2422,11 @@ func TestOpenIDConnectRolesChannelsClaims(t *testing.T) {
client := &http.Client{Jar: jar}
response, err := client.Do(request)
require.NoError(t, err, "Error sending request")
defer func() { assert.NoError(t, response.Body.Close()) }()
// Validate received token response
require.Equal(t, http.StatusOK, response.StatusCode)
var authResponseActual OIDCTokenResponse
require.NoError(t, err, json.NewDecoder(response.Body).Decode(&authResponseActual))
require.NoError(t, response.Body.Close(), "Error closing response body")
assert.NotEmpty(t, authResponseActual.SessionID, "session_id doesn't exist")

authResponseExpected := mockAuthServer.options.tokenResponse
Expand Down
21 changes: 9 additions & 12 deletions rest/utilities_testing_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,20 @@ func MakeUser(t *testing.T, httpClient *http.Client, serverURL, username, passwo

resp, err := httpClient.Do(req)
if err != nil {
return true, err, resp
return true, err, nil
}
assert.NoError(t, resp.Body.Close())
return false, err, resp
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyResp, err := io.ReadAll(resp.Body)
assert.NoError(t, err, "Failed to create user: %s", bodyResp)
}
require.Equal(t, http.StatusOK, resp.StatusCode)
return false, err, nil
}

err, resp := base.RetryLoop(base.TestCtx(t), "Admin Auth testing MakeUser", retryWorker, base.CreateSleeperFunc(10, 100))
err, _ := base.RetryLoop(base.TestCtx(t), "Admin Auth testing MakeUser", retryWorker, base.CreateSleeperFunc(10, 100))
require.NoError(t, err)

if resp.(*http.Response).StatusCode != http.StatusOK {
bodyResp, err := io.ReadAll(resp.(*http.Response).Body)
assert.NoError(t, err)
fmt.Println(string(bodyResp))
}
require.Equal(t, http.StatusOK, resp.(*http.Response).StatusCode)

require.NoError(t, resp.(*http.Response).Body.Close(), "Error closing response body")
}

func DeleteUser(t *testing.T, httpClient *http.Client, serverURL, username string) {
Expand Down

0 comments on commit 0215087

Please sign in to comment.