Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Add clientapi tests #2916

Merged
merged 10 commits into from
Dec 23, 2022
Merged

Add clientapi tests #2916

merged 10 commits into from
Dec 23, 2022

Conversation

S7evinK
Copy link
Contributor

@S7evinK S7evinK commented Dec 19, 2022

This PR

  • adds several tests for the clientapi, mostly around /register and auth fallback.
  • removes the now deprecated homeserver field from responses to /register
  • slightly refactors auth fallback handling

@S7evinK S7evinK added tests Issues related to tests. Missing/Flakey/etc T-Task Refactoring, removal, replacement, enabling or disabling functionality, other engineering tasks. T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues. labels Dec 19, 2022
@S7evinK S7evinK requested a review from a team as a code owner December 19, 2022 14:44
@codecov
Copy link

codecov bot commented Dec 19, 2022

Codecov Report

Base: 35.75% // Head: 36.47% // Increases project coverage by +0.72% 🎉

Coverage data is based on head (508e4c6) compared to base (5eed31f).
Patch coverage: 76.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2916      +/-   ##
==========================================
+ Coverage   35.75%   36.47%   +0.72%     
==========================================
  Files         494      494              
  Lines       54673    54657      -16     
==========================================
+ Hits        19549    19937     +388     
+ Misses      32573    32149     -424     
- Partials     2551     2571      +20     
Flag Coverage Δ
unittests 36.47% <76.00%> (+0.72%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
clientapi/routing/login.go 42.62% <ø> (-0.93%) ⬇️
clientapi/routing/password.go 0.00% <0.00%> (ø)
clientapi/routing/routing.go 16.97% <0.00%> (ø)
cmd/create-account/main.go 8.82% <0.00%> (-0.07%) ⬇️
internal/httputil/httpapi.go 42.13% <50.00%> (+0.63%) ⬆️
clientapi/routing/register.go 60.03% <55.55%> (+39.63%) ⬆️
clientapi/routing/auth_fallback.go 81.70% <81.25%> (+81.70%) ⬆️
clientapi/routing/admin.go 26.54% <100.00%> (ø)
internal/validate.go 100.00% <100.00%> (ø)
setup/config/config.go 39.73% <100.00%> (+2.10%) ⬆️
... and 24 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@S7evinK
Copy link
Contributor Author

S7evinK commented Dec 20, 2022

Needs matrix-org/complement#573 for Complement to pass, as this also removes the deprecated (since r0.4.0) home_server field from responses to /register and /login.

Copy link
Member

@kegsay kegsay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly small things, the main code here is good, thanks!

@@ -93,6 +90,10 @@ func (c *ClientAPI) Verify(configErrs *ConfigErrors, isMonolith bool) {
if c.RecaptchaSitekeyClass == "" {
c.RecaptchaSitekeyClass = "g-recaptcha-response"
}
checkNotEmpty(configErrs, "client_api.recaptcha_public_key", c.RecaptchaPublicKey)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why shift the order of checks here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could remove it completely, given we either have something set or default to reCaptcha (if nothing is set).

clientapi/routing/auth_fallback.go Show resolved Hide resolved
clientapi/routing/auth_fallback.go Outdated Show resolved Hide resolved

serveSuccess()
return nil
clientIP := req.RemoteAddr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to honour the real IP header in case of reverse proxies. It's a config option iirc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, but unfortunately just for the sync api :/

err := req.ParseForm()
if err != nil {
util.GetLogger(req.Context()).WithError(err).Error("req.ParseForm failed")
res := jsonerror.InternalServerError()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most certainly not a 500, surely 4xx?

{
name: "valid username",
localpart: "valid",
domain: "localhost",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surprised we don't ever test bad domains.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also had that added, just to see that we either use the server_name from the configuration OR one from a virtual host.
And yea, something like http://localhost would produce a valid username

},
},
{
name: "invalid username",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Say why (caps).

Also, it would be good to add tests for:

  • unicode characters e.g emoji
  • ASCII symbols e.g $
  • a complex but valid username beyond the existing test for "valid" e.g f00_bar-baz.=40/ is valid I think? Test it.

}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validateUsername(tt.localpart, tt.domain); !reflect.DeepEqual(got, tt.want) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst we are here... please move validateUsername to internal or some other package and call it from cmd/create-account to prevent people registering usernames with caps or other things like that.

t.Errorf("validateUsername() = %v, want %v", got, tt.want)
}
if got := validateApplicationServiceUsername(tt.localpart, tt.domain); !reflect.DeepEqual(got, tt.want) {
if got != nil && got.JSON != jsonerror.InvalidUsername("Username cannot start with a '_'") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs more comments to explain why you're doing this check. I assume because appservices are allowed to use _.

password: "shortpw",
want: &util.JSONResponse{
Code: http.StatusBadRequest,
JSON: jsonerror.WeakPassword(fmt.Sprintf("password too weak: min %d chars", minPasswordLength)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I generally dislike doing direct checks on error strings, as they aren't really part of the public API. We should be checking the errcode. Write a helper function and gjson out the code? This applies throughout these tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like above, we should just remove the JSON here and actually return error types. (Same for the other validate* functions)

Copy link
Member

@kegsay kegsay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@S7evinK S7evinK merged commit f762ce1 into main Dec 23, 2022
@S7evinK S7evinK deleted the s7evink/registertests branch December 23, 2022 13:11
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
T-Defect Bugs, crashes, hangs, security vulnerabilities, or other reported issues. T-Task Refactoring, removal, replacement, enabling or disabling functionality, other engineering tasks. tests Issues related to tests. Missing/Flakey/etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants