Skip to content

Comments

feat: Add comprehensive unit tests and refactor for testability#4

Open
acramatte wants to merge 1 commit intomainfrom
add-unit-tests
Open

feat: Add comprehensive unit tests and refactor for testability#4
acramatte wants to merge 1 commit intomainfrom
add-unit-tests

Conversation

@acramatte
Copy link
Owner

This commit introduces unit tests for major parts of the application, including authentication logic, chirp handlers, and login handlers.

Key changes:

  • Added unit tests for internal/auth/auth.go, covering password hashing, JWT operations, and token/API key extraction.
  • Added unit tests for handler_chirps.go, including chirp creation, retrieval, deletion, and body cleaning. This required:
    • Modifying handler_chirps.go to use chi.URLParam() for path parameters to enable easier testing.
  • Added unit tests for handler_login.go, covering user login, token refresh, and token revocation.

To support testability, the following refactoring was performed:

  • The db field in main.apiConfig was changed from a concrete *database.Queries type to a database.Querier interface. A Querier interface was defined in internal/database/db.go encompassing all necessary database methods.
  • Key authentication functions (ValidateJWT, GetBearerToken, GetAPIKey) were made injectable by adding them as fields to main.apiConfig. Handler functions were updated to use these injectable functions.

Finally, README.md was updated with a "Running Tests" section, instructing you on how to execute the test suite using go test ./....

The tests for GetAPIKey and GetBearerToken in internal/auth/auth_test.go correctly fail for specific edge cases where the token/key string is empty after the prefix, as the underlying functions do not currently return errors for these inputs. These functions were not modified as part of this work.

This commit introduces unit tests for major parts of the application, including authentication logic, chirp handlers, and login handlers.

Key changes:
- Added unit tests for `internal/auth/auth.go`, covering password hashing, JWT operations, and token/API key extraction.
- Added unit tests for `handler_chirps.go`, including chirp creation, retrieval, deletion, and body cleaning. This required:
    - Modifying `handler_chirps.go` to use `chi.URLParam()` for path parameters to enable easier testing.
- Added unit tests for `handler_login.go`, covering user login, token refresh, and token revocation.

To support testability, the following refactoring was performed:
- The `db` field in `main.apiConfig` was changed from a concrete `*database.Queries` type to a `database.Querier` interface. A `Querier` interface was defined in `internal/database/db.go` encompassing all necessary database methods.
- Key authentication functions (`ValidateJWT`, `GetBearerToken`, `GetAPIKey`) were made injectable by adding them as fields to `main.apiConfig`. Handler functions were updated to use these injectable functions.

Finally, `README.md` was updated with a "Running Tests" section, instructing you on how to execute the test suite using `go test ./...`.

The tests for `GetAPIKey` and `GetBearerToken` in `internal/auth/auth_test.go` correctly fail for specific edge cases where the token/key string is empty after the prefix, as the underlying functions do not currently return errors for these inputs. These functions were not modified as part of this work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant