fix: remove the unused user use cases, fix the specs that no longer match the code, include api/middleware and api/pkg/claims in mutation testing, log failures that password reset requests currently ignore, and reject user_id claims that don't fit a user id
This commit is contained in:
1 parent
cbb403011a
commit
3282b0580c
22 files changed
+260
-314
No files matched your search
@@ -14,7 +14,6 @@ import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
@@ -77,64 +76,6 @@ func TestCreateUserUseCaseHashesPasswordAndVerificationCode(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpdateUserUseCaseHashesUpdatedValues(t *testing.T) {
|
||||
cases := []struct {
|
||||
method string
|
||||
plaintext string
|
||||
run func(uc *useCases.UpdateUserUseCase, plaintext string) (user.User, error)
|
||||
stored func(u user.User) string
|
||||
}{
|
||||
{
|
||||
method: "UpdatePassword",
|
||||
plaintext: "newPassw0rd",
|
||||
run: func(uc *useCases.UpdateUserUseCase, plaintext string) (user.User, error) {
|
||||
return uc.UpdatePassword(1, plaintext)
|
||||
},
|
||||
stored: func(u user.User) string { return u.Password },
|
||||
},
|
||||
{
|
||||
method: "UpdatePasswordResetCode",
|
||||
plaintext: "reset-code-123",
|
||||
run: func(uc *useCases.UpdateUserUseCase, plaintext string) (user.User, error) {
|
||||
return uc.UpdatePasswordResetCode(1, plaintext, time.Now().Add(time.Hour))
|
||||
},
|
||||
stored: func(u user.User) string { return u.PasswordResetCode },
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.method+" stores the hashed value", func(t *testing.T) {
|
||||
repo := newMockUserRepo()
|
||||
repo.users[1] = user.User{ID: 1}
|
||||
uc := useCases.NewUpdateUserUseCase(repo, &stubHasher{})
|
||||
|
||||
_, err := tc.run(uc, tc.plaintext)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
|
||||
expected := "hashed:" + tc.plaintext
|
||||
if got := tc.stored(repo.users[1]); got != expected {
|
||||
t.Errorf("expected stored value %q, got %q", expected, got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run(tc.method+" a hashing failure persists nothing", func(t *testing.T) {
|
||||
repo := newMockUserRepo()
|
||||
repo.users[1] = user.User{ID: 1}
|
||||
uc := useCases.NewUpdateUserUseCase(repo, &stubHasher{hashErr: errStubHashFailure})
|
||||
|
||||
_, err := tc.run(uc, tc.plaintext)
|
||||
if !errors.Is(err, errStubHashFailure) {
|
||||
t.Errorf("expected the hashing error, got %v", err)
|
||||
}
|
||||
if got := tc.stored(repo.users[1]); got != "" {
|
||||
t.Errorf("expected the repository update to never be called, got %q", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthenticateUserUseCase(t *testing.T) {
|
||||
newRepo := func() *mockUserRepo {
|
||||
repo := newMockUserRepo()
|
||||
|
||||
Reference in new issue
Block a user