Skip to content

Add comprehensive unit tests for modules/hibp - #2165

Open
Seanstoppable wants to merge 1 commit into
wtfutil:trunkfrom
Seanstoppable:seanstoppable-hibp-test-coverage
Open

Add comprehensive unit tests for modules/hibp#2165
Seanstoppable wants to merge 1 commit into
wtfutil:trunkfrom
Seanstoppable:seanstoppable-hibp-test-coverage

Conversation

@Seanstoppable

Copy link
Copy Markdown
Collaborator

Summary

Adds unit tests for the modules/hibp (Have I Been Pwned) package, which previously had 0% test coverage.

Coverage: 0% -> 94.2%

What's tested

  • Status/Breach: NewStatus, HasBeenCompromised, Len, BreachDate (table-driven, including malformed dates)
  • Settings: HasSince, SinceDate, NewSettingsFromYAML (colors, since, refresh-interval floor), ConfigText
  • Widget: sinceDateForTitle, content() (color/status logic, error state, since-date title), Fetch, Refresh, NewWidget
  • Client (mocked with net/http/httptest): fullURL, validateHTTPResponse, parseResponseBody, filterBreaches, and fetchForAccount covering success, 404/no-breach, rate-limit (429), 401/402 API errors, malformed JSON, and simulated network errors

Notable changes

  • Made apiURL a package variable (was a const) so tests can redirect requests to an httptest server.
  • Fixed a real bug found while testing error paths: fetchForAccount was swallowing network/read errors - it checked getErr/readErr but returned the already-nil outer err, so network failures silently returned (nil, nil) instead of an error. Also added a missing response.Body.Close().

Validation

  • go build ./... passes
  • go test ./modules/hibp/... -cover passes (94.2% coverage)
  • golangci-lint run ./modules/hibp/... reports no real issues; only gofmt warnings remain, which are pre-existing CRLF line-ending artifacts reproducible on unrelated, untouched modules (e.g. modules/clocks) - out of scope for this change.

- Add table-driven tests for Status, Breach, Settings, Widget content/title formatting
- Add httptest-backed tests for HIBP API client covering success, empty (404),
  rate limiting, unauthorized/error responses, malformed JSON, and network errors
- Make apiURL a variable so tests can point it at an httptest server
- Fix fetchForAccount swallowing network/read errors (was returning the wrong,
  already-nil err variable instead of getErr/readErr)
- Coverage for modules/hibp: 0% -> 94.2%
- Verified with golangci-lint; fixed errcheck findings in test HTTP handlers.
  Remaining gofmt warnings are pre-existing repo-wide CRLF line-ending artifacts,
  reproducible on unrelated untouched modules, and out of scope.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant