Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up auth package tests by setting lower test-only bcrypt cost #6682

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 57 additions & 58 deletions auth/auth_test.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion auth/auth_time_sensitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestAuthenticationSpeed(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))
user, _ := auth.NewUser("me", "goIsKewl", nil)
assert.True(t, user.Authenticate("goIsKewl"))

Expand Down
4 changes: 2 additions & 2 deletions auth/collection_access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestUserCollectionAccess(t *testing.T) {
"collection1": struct{}{},
},
}
auth := NewAuthenticator(bucket.GetSingleDataStore(), nil, options)
auth := NewTestAuthenticator(t, bucket.GetSingleDataStore(), nil, options)
user, _ := auth.NewUser("foo", "password", nil)
scope := "scope1"
collection := "collection1"
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestSerializeUserWithCollections(t *testing.T) {
ctx := base.TestCtx(t)
bucket := base.GetTestBucket(t)
defer bucket.Close(ctx)
auth := NewAuthenticator(bucket.GetSingleDataStore(), nil, DefaultAuthenticatorOptions(base.TestCtx(t)))
auth := NewTestAuthenticator(t, bucket.GetSingleDataStore(), nil, DefaultAuthenticatorOptions(base.TestCtx(t)))
user, _ := auth.NewUser("me", "letmein", ch.BaseSetOf(t, "me", "public"))
encoded, err := base.JSONMarshal(user)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion auth/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,7 @@ func TestJWTRolesChannels(t *testing.T) {
roleChannels: map[string]ch.TimedSet{},
}

auth := NewAuthenticator(dataStore, &testMockComputer, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, &testMockComputer, DefaultAuthenticatorOptions(ctx))

provider := &OIDCProvider{
Name: "foo",
Expand Down
2 changes: 2 additions & 0 deletions auth/password_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func TestSetBcryptCost(t *testing.T) {
bucket := base.GetTestBucket(t)
defer bucket.Close(ctx)
dataStore := bucket.GetSingleDataStore()

// Not NewTestAuthenticator, since we're testing BcryptCost and want the actual bcrypt default.
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))

err := auth.SetBcryptCost(DefaultBcryptCost - 1) // below minimum allowed value
Expand Down
6 changes: 3 additions & 3 deletions auth/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestAuthorizeChannelsRole(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

role, err := auth.NewRole("root", channels.BaseSetOf(t, "superuser"))
assert.NoError(t, err)
Expand All @@ -67,12 +67,12 @@ func TestRoleKeysHash(t *testing.T) {
defer testBucket.Close(ctx)
dataStore := testBucket.DefaultDataStore()

auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))
if !metadataDefault {
namedMetadataOptions := DefaultAuthenticatorOptions(ctx)
namedMetadataOptions.MetaKeys = base.NewMetadataKeys("foo")

auth = NewAuthenticator(dataStore, nil, namedMetadataOptions)
auth = NewTestAuthenticator(t, dataStore, nil, namedMetadataOptions)

}
shortRoleName := "shortRole"
Expand Down
14 changes: 7 additions & 7 deletions auth/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestCreateSession(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

user, err := auth.NewUser(username, "password", base.Set{})
require.NoError(t, err)
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestDeleteSession(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

id, err := base.GenerateRandomSecret()
require.NoError(t, err)
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestMakeSessionCookie(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

sessionID, err := base.GenerateRandomSecret()
require.NoError(t, err)
Expand All @@ -133,7 +133,7 @@ func TestMakeSessionCookieProperties(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

sessionID, err := base.GenerateRandomSecret()
require.NoError(t, err)
Expand Down Expand Up @@ -169,7 +169,7 @@ func TestDeleteSessionForCookie(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

sessionID, err := base.GenerateRandomSecret()
require.NoError(t, err)
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestCreateSessionChangePassword(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

user, err := auth.NewUser(test.username, test.password, base.Set{})
require.NoError(t, err)
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestUserWithoutSessionUUID(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))
const username = "Alice"
user, err := auth.NewUser(username, "password", base.Set{})
require.NoError(t, err)
Expand Down
23 changes: 13 additions & 10 deletions auth/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestUserAuthenticateDisabled(t *testing.T) {
defer bucket.Close(ctx)
dataStore := bucket.GetSingleDataStore()
// Create user
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))
u, err := auth.NewUser(username, oldPassword, base.Set{})
assert.NoError(t, err)
assert.NotNil(t, u)
Expand Down Expand Up @@ -70,8 +70,11 @@ func TestUserAuthenticatePasswordHashUpgrade(t *testing.T) {
bucket := base.GetTestBucket(t)
defer bucket.Close(ctx)
dataStore := bucket.GetSingleDataStore()
// Create user

// Not NewTestAuthenticator, since we're testing BcryptCost and want the actual bcrypt default.
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))

// Create user
u, err := auth.NewUser(username, oldPassword, base.Set{})
require.NoError(t, err)
require.NotNil(t, u)
Expand Down Expand Up @@ -254,7 +257,7 @@ func TestCanSeeChannelSince(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))
freeChannels := base.SetFromArray([]string{"ESPN", "HBO", "FX", "AMC"})
user, err := auth.NewUser("user", "password", freeChannels)
assert.Nil(t, err)
Expand Down Expand Up @@ -283,7 +286,7 @@ func TestGetAddedChannels(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

role, err := auth.NewRole("music", channels.BaseSetOf(t, "Spotify", "Youtube"))
assert.Nil(t, err)
Expand Down Expand Up @@ -327,7 +330,7 @@ func TestUserAuthenticateWithDisabledUserAccount(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

user, err := auth.NewUser(username, password, base.Set{})
assert.NoError(t, err)
Expand All @@ -350,7 +353,7 @@ func TestUserAuthenticateWithOldPasswordHash(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

user, err := auth.NewUser(username, password, base.Set{})
assert.NoError(t, err)
Expand All @@ -372,7 +375,7 @@ func TestUserAuthenticateWithBadPasswordHash(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

user, err := auth.NewUser(username, password, base.Set{})
assert.NoError(t, err)
Expand All @@ -394,7 +397,7 @@ func TestUserAuthenticateWithNoHashAndBadPassword(t *testing.T) {
testBucket := base.GetTestBucket(t)
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()
auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))

user, err := auth.NewUser(username, password, base.Set{})
assert.NoError(t, err)
Expand All @@ -412,12 +415,12 @@ func TestUserKeysHash(t *testing.T) {
defer testBucket.Close(ctx)
dataStore := testBucket.GetSingleDataStore()

auth := NewAuthenticator(dataStore, nil, DefaultAuthenticatorOptions(ctx))
auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(ctx))
if !metadataDefault {
namedMetadataOptions := DefaultAuthenticatorOptions(ctx)
namedMetadataOptions.MetaKeys = base.NewMetadataKeys("foo")

auth = NewAuthenticator(testBucket.GetSingleDataStore(), nil, namedMetadataOptions)
auth = NewTestAuthenticator(t, testBucket.GetSingleDataStore(), nil, namedMetadataOptions)

}
bobUsername := "bob"
Expand Down
Loading