(t *testing.T, ctx context.Context, s *UsersStore)
| 131 | } |
| 132 | |
| 133 | func usersAuthenticate(t *testing.T, ctx context.Context, s *UsersStore) { |
| 134 | password := "pa$$word" |
| 135 | alice, err := s.Create(ctx, "alice", "alice@example.com", |
| 136 | CreateUserOptions{ |
| 137 | Password: password, |
| 138 | }, |
| 139 | ) |
| 140 | require.NoError(t, err) |
| 141 | |
| 142 | t.Run("user not found", func(t *testing.T) { |
| 143 | _, err := s.Authenticate(ctx, "bob", password, -1) |
| 144 | wantErr := auth.ErrBadCredentials{Args: map[string]any{"login": "bob"}} |
| 145 | assert.Equal(t, wantErr, err) |
| 146 | }) |
| 147 | |
| 148 | t.Run("invalid password", func(t *testing.T) { |
| 149 | _, err := s.Authenticate(ctx, alice.Name, "bad_password", -1) |
| 150 | wantErr := auth.ErrBadCredentials{Args: map[string]any{"login": alice.Name, "userID": alice.ID}} |
| 151 | assert.Equal(t, wantErr, err) |
| 152 | }) |
| 153 | |
| 154 | t.Run("via email and password", func(t *testing.T) { |
| 155 | user, err := s.Authenticate(ctx, alice.Email, password, -1) |
| 156 | require.NoError(t, err) |
| 157 | assert.Equal(t, alice.Name, user.Name) |
| 158 | }) |
| 159 | |
| 160 | t.Run("via username and password", func(t *testing.T) { |
| 161 | user, err := s.Authenticate(ctx, alice.Name, password, -1) |
| 162 | require.NoError(t, err) |
| 163 | assert.Equal(t, alice.Name, user.Name) |
| 164 | }) |
| 165 | |
| 166 | t.Run("login source mismatch", func(t *testing.T) { |
| 167 | _, err := s.Authenticate(ctx, alice.Email, password, 1) |
| 168 | gotErr := fmt.Sprintf("%v", err) |
| 169 | wantErr := ErrLoginSourceMismatch{args: map[string]any{"actual": 0, "expect": 1}}.Error() |
| 170 | assert.Equal(t, wantErr, gotErr) |
| 171 | }) |
| 172 | |
| 173 | t.Run("via login source", func(t *testing.T) { |
| 174 | loginSourcesStore := newLoginSourcesStore(s.db, NewMockLoginSourceFilesStore()) |
| 175 | loginSource, err := loginSourcesStore.Create( |
| 176 | ctx, |
| 177 | CreateLoginSourceOptions{ |
| 178 | Type: auth.Mock, |
| 179 | Name: "mock-1", |
| 180 | Activated: true, |
| 181 | Config: mockProviderConfig{ |
| 182 | ExternalAccount: &auth.ExternalAccount{}, |
| 183 | }, |
| 184 | }, |
| 185 | ) |
| 186 | require.NoError(t, err) |
| 187 | |
| 188 | bob, err := s.Create(ctx, "bob", "bob@example.com", |
| 189 | CreateUserOptions{ |
| 190 | Password: password, |
nothing calls this directly
no test coverage detected