MCPcopy Index your code
hub / github.com/gogs/gogs / usersGetByEmail

Function usersGetByEmail

internal/database/users_test.go:720–779  ·  view source on GitHub ↗
(t *testing.T, ctx context.Context, s *UsersStore)

Source from the content-addressed store, hash-verified

718}
719
720func usersGetByEmail(t *testing.T, ctx context.Context, s *UsersStore) {
721 t.Run("empty email", func(t *testing.T) {
722 _, err := s.GetByEmail(ctx, "")
723 wantErr := ErrUserNotExist{args: errutil.Args{"email": ""}}
724 assert.Equal(t, wantErr, err)
725 })
726
727 t.Run("ignore organization", func(t *testing.T) {
728 // TODO: Use Orgs.Create to replace SQL hack when the method is available.
729 org, err := s.Create(ctx, "gogs", "gogs@example.com", CreateUserOptions{})
730 require.NoError(t, err)
731
732 err = s.db.Model(&User{}).Where("id", org.ID).UpdateColumn("type", UserTypeOrganization).Error
733 require.NoError(t, err)
734
735 _, err = s.GetByEmail(ctx, org.Email)
736 wantErr := ErrUserNotExist{args: errutil.Args{"email": org.Email}}
737 assert.Equal(t, wantErr, err)
738 })
739
740 t.Run("by primary email", func(t *testing.T) {
741 alice, err := s.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
742 require.NoError(t, err)
743
744 _, err = s.GetByEmail(ctx, alice.Email)
745 wantErr := ErrUserNotExist{args: errutil.Args{"email": alice.Email}}
746 assert.Equal(t, wantErr, err)
747
748 // Mark user as activated
749 // TODO: Use UserEmails.Verify to replace SQL hack when the method is available.
750 err = s.db.Model(&User{}).Where("id", alice.ID).UpdateColumn("is_active", true).Error
751 require.NoError(t, err)
752
753 user, err := s.GetByEmail(ctx, alice.Email)
754 require.NoError(t, err)
755 assert.Equal(t, alice.Name, user.Name)
756 })
757
758 t.Run("by secondary email", func(t *testing.T) {
759 bob, err := s.Create(ctx, "bob", "bob@example.com", CreateUserOptions{})
760 require.NoError(t, err)
761
762 // TODO: Use UserEmails.Create to replace SQL hack when the method is available.
763 email2 := "bob2@example.com"
764 err = s.db.Exec(`INSERT INTO email_address (uid, email) VALUES (?, ?)`, bob.ID, email2).Error
765 require.NoError(t, err)
766
767 _, err = s.GetByEmail(ctx, email2)
768 wantErr := ErrUserNotExist{args: errutil.Args{"email": email2}}
769 assert.Equal(t, wantErr, err)
770
771 // TODO: Use UserEmails.Verify to replace SQL hack when the method is available.
772 err = s.db.Exec(`UPDATE email_address SET is_activated = ? WHERE email = ?`, true, email2).Error
773 require.NoError(t, err)
774
775 user, err := s.GetByEmail(ctx, email2)
776 require.NoError(t, err)
777 assert.Equal(t, bob.Name, user.Name)

Callers

nothing calls this directly

Calls 4

GetByEmailMethod · 0.80
WhereMethod · 0.80
ExecMethod · 0.80
CreateMethod · 0.45

Tested by

no test coverage detected