MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / TestCASUpdatePrincipal

Function TestCASUpdatePrincipal

auth/auth_test.go:635–699  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

633}
634
635func TestCASUpdatePrincipal(t *testing.T) {
636 ctx := base.TestCtx(t)
637 bucket := base.GetTestBucket(t)
638 defer bucket.Close(ctx)
639
640 dataStore := bucket.GetSingleDataStore()
641
642 // Create user
643 username := "foo"
644 password := "password"
645 email := "foo@bar.org"
646
647 // Create user
648 auth := NewTestAuthenticator(t, dataStore, nil, DefaultAuthenticatorOptions(base.TestCtx(t)))
649
650 // Modify the bcrypt cost to test rehashPassword properly below
651 require.Error(t, auth.SetBcryptCost(5))
652
653 user, err := auth.NewUser(username, password, ch.BaseSetOf(t, "123", "456"))
654 require.NoError(t, err)
655 user.SetExplicitRoles(ch.TimedSet{"role1": ch.NewVbSimpleSequence(1), "role2": ch.NewVbSimpleSequence(1)}, 1)
656 require.NoError(t, auth.Save(user))
657 user, err = auth.GetUser(username)
658 require.NoError(t, err)
659 require.NotNilf(t, user, "User is nil prior to invalidate channels: %v", err)
660
661 // updateEmailWithConflictCallback causes CAS failure three times
662 updateCount := uint64(0)
663 updateEmailWithConflictCallback := func(currentPrincipal Principal) (updatedPrincipal Principal, err error) {
664 currentUser, ok := currentPrincipal.(User)
665 if !ok {
666 return nil, base.ErrUpdateCancel
667 }
668
669 log.Printf("attempting update with CAS:%v", currentUser.Cas())
670 if updateCount < 3 {
671 // Update principal externally to trigger CAS error three times
672 concurrentUser, err := auth.GetUser(username)
673 assert.NoError(t, err)
674 log.Printf("setting explicit channels to %v", updateCount)
675 concurrentUser.SetExplicitChannels(ch.TimedSet{"ch1": ch.NewVbSimpleSequence(updateCount)}, updateCount)
676 updateErr := auth.Save(concurrentUser)
677 assert.NoError(t, updateErr)
678
679 updatedConcurrentUser, err := auth.GetUser(username)
680 assert.NoError(t, err)
681 log.Printf("Forcing cas failure, updated CAS: %v", updatedConcurrentUser.Cas())
682
683 updateCount++
684 }
685
686 err = currentUser.SetEmail(email)
687 if err != nil {
688 return nil, err
689 }
690 return currentUser, nil
691 }
692

Callers

nothing calls this directly

Calls 15

SetExplicitRolesMethod · 0.95
TestCtxFunction · 0.92
GetTestBucketFunction · 0.92
NewTestAuthenticatorFunction · 0.85
SetBcryptCostMethod · 0.80
NewUserMethod · 0.80
SaveMethod · 0.80
GetUserMethod · 0.80
casUpdatePrincipalMethod · 0.80
CloseMethod · 0.65
CasMethod · 0.65

Tested by

no test coverage detected