feat: Add comprehensive unit tests and refactor for testability#4
Open
feat: Add comprehensive unit tests and refactor for testability#4
Conversation
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit introduces unit tests for major parts of the application, including authentication logic, chirp handlers, and login handlers.
Key changes:
internal/auth/auth.go, covering password hashing, JWT operations, and token/API key extraction.handler_chirps.go, including chirp creation, retrieval, deletion, and body cleaning. This required:handler_chirps.goto usechi.URLParam()for path parameters to enable easier testing.handler_login.go, covering user login, token refresh, and token revocation.To support testability, the following refactoring was performed:
dbfield inmain.apiConfigwas changed from a concrete*database.Queriestype to adatabase.Querierinterface. AQuerierinterface was defined ininternal/database/db.goencompassing all necessary database methods.ValidateJWT,GetBearerToken,GetAPIKey) were made injectable by adding them as fields tomain.apiConfig. Handler functions were updated to use these injectable functions.Finally,
README.mdwas updated with a "Running Tests" section, instructing you on how to execute the test suite usinggo test ./....The tests for
GetAPIKeyandGetBearerTokenininternal/auth/auth_test.gocorrectly 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.