Skip to content

Commit

Permalink
fix: data race in test
Browse files Browse the repository at this point in the history
  • Loading branch information
alnr committed Oct 11, 2023
1 parent 976cd0d commit ab6dc31
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions selfservice/strategy/password/op_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"net/http"
"net/url"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -180,7 +181,7 @@ func TestOAuth2Provider(t *testing.T) {

redirTS := testhelpers.NewRedirSessionEchoTS(t, reg)

oAuthSuccess := false
var oAuthSuccess atomic.Bool
var hydraAdminClient hydraclientgo.OAuth2Api
var clientAppOAuth2Config *oauth2.Config

Expand All @@ -191,7 +192,7 @@ func TestOAuth2Provider(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, token)
require.NotEqual(t, "", token.AccessToken)
oAuthSuccess = true
oAuthSuccess.Store(true)
t.Log("[clientAppTS] successfully exchanged code for token")
} else {
t.Error("[clientAppTS] code query parameter is missing")
Expand All @@ -200,7 +201,8 @@ func TestOAuth2Provider(t *testing.T) {

identifier, pwd := x.NewUUID().String(), "password"

testRequireLogin := true
var testRequireLogin atomic.Bool
testRequireLogin.Store(true)

uiTS := testhelpers.NewHTTPTestServer(t, http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
t.Logf("[uiTS] handling %s", r.URL)
Expand All @@ -209,8 +211,8 @@ func TestOAuth2Provider(t *testing.T) {
if len(q) == 1 && !q.Has("flow") && q.Has("login_challenge") {
t.Log("[uiTS] initializing a new OpenID Provider flow")
hlc := r.URL.Query().Get("login_challenge")
f := testhelpers.InitializeLoginFlowViaBrowser(t, browserClient, kratosPublicTS, false, false, false, !testRequireLogin, testhelpers.InitFlowWithOAuth2LoginChallenge(hlc))
if testRequireLogin {
f := testhelpers.InitializeLoginFlowViaBrowser(t, browserClient, kratosPublicTS, false, false, false, !testRequireLogin.Load(), testhelpers.InitFlowWithOAuth2LoginChallenge(hlc))
if testRequireLogin.Load() {
require.NotNil(t, f)

values := url.Values{"method": {"password"}, "identifier": {identifier}, "password": {pwd}, "csrf_token": {x.FakeCSRFToken}}.Encode()
Expand Down Expand Up @@ -284,8 +286,8 @@ func TestOAuth2Provider(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "", string(body))
require.Equal(t, http.StatusOK, res.StatusCode)
require.True(t, oAuthSuccess)
oAuthSuccess = false
require.True(t, oAuthSuccess.Load())
oAuthSuccess.Store(false)
})

conf.MustSet(ctx, config.ViperKeySessionPersistentCookie, true)
Expand All @@ -299,11 +301,11 @@ func TestOAuth2Provider(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "", string(body))
require.Equal(t, http.StatusOK, res.StatusCode)
require.True(t, oAuthSuccess)
oAuthSuccess = false
require.True(t, oAuthSuccess.Load())
oAuthSuccess.Store(false)
})

testRequireLogin = false
testRequireLogin.Store(false)
t.Run("should prompt the user for consent, but not for login", func(t *testing.T) {
authCodeURL := makeAuthCodeURL(t, clientAppOAuth2Config, "", false)
res, err := browserClient.Get(authCodeURL)
Expand All @@ -314,8 +316,8 @@ func TestOAuth2Provider(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "", string(body))
require.Equal(t, http.StatusOK, res.StatusCode)
require.True(t, oAuthSuccess)
oAuthSuccess = false
require.True(t, oAuthSuccess.Load())
oAuthSuccess.Store(false)
})

reg.WithHydra(&AcceptWrongSubject{h: reg.Hydra().(*hydra.DefaultHydra)})
Expand All @@ -329,8 +331,8 @@ func TestOAuth2Provider(t *testing.T) {
require.NoError(t, err)
require.Equal(t, "", string(body))
require.Equal(t, http.StatusOK, res.StatusCode)
require.False(t, oAuthSuccess)
oAuthSuccess = false
require.False(t, oAuthSuccess.Load())
oAuthSuccess.Store(false)
})
}

Expand Down

0 comments on commit ab6dc31

Please sign in to comment.