mas: refactoring

This commit is contained in:
Roman Isaev 2025-01-15 19:18:29 +00:00
parent 418c584e40
commit 3619a6de8d
No known key found for this signature in database
GPG key ID: 7BE2B6A6C89AEC7F
2 changed files with 97 additions and 81 deletions

View file

@ -2161,7 +2161,7 @@ func TestAdminCreateOrModifyAccount(t *testing.T) {
ThreePIDs []string ThreePIDs []string
}{ }{
// In order to avoid any confusion and undesired behaviour, we do not change display name and avatar url if account already exists // In order to avoid any confusion and undesired behaviour, we do not change display name and avatar url if account already exists
DisplayName: "1", DisplayName: alice.Localpart,
AvatarURL: "", AvatarURL: "",
ThreePIDs: []string{"alice@example.com"}, ThreePIDs: []string{"alice@example.com"},
}, },
@ -2291,32 +2291,42 @@ func TestAdminRetrieveAccount(t *testing.T) {
} }
}) })
testCase := []struct {
Name string
User *test.User
Code int
Body string
}{
{
Name: "Retrieve existing account",
User: alice,
Code: http.StatusOK,
Body: fmt.Sprintf(`{"display_name":"%s","avatar_url":"","deactivated":false}`, alice.Localpart),
},
{
Name: "Retrieve non-existing account",
User: bob,
Code: http.StatusNotFound,
Body: "",
},
}
for _, tc := range testCase {
t.Run("Retrieve existing account", func(t *testing.T) { t.Run("Retrieve existing account", func(t *testing.T) {
req := test.NewRequest(t, http.MethodGet, "/_synapse/admin/v2/users/"+alice.ID) req := test.NewRequest(t, http.MethodGet, "/_synapse/admin/v2/users/"+tc.User.ID)
req.Header.Set("Authorization", "Bearer "+adminToken) req.Header.Set("Authorization", "Bearer "+adminToken)
rec := httptest.NewRecorder() rec := httptest.NewRecorder()
routers.SynapseAdmin.ServeHTTP(rec, req) routers.SynapseAdmin.ServeHTTP(rec, req)
t.Logf("%s", rec.Body.String()) t.Logf("%s", rec.Body.String())
if rec.Code != http.StatusOK { if rec.Code != tc.Code {
t.Fatalf("expected HTTP status %d, got %d: %s", http.StatusOK, rec.Code, rec.Body.String()) t.Fatalf("expected HTTP status %d, got %d: %s", tc.Code, rec.Code, rec.Body.String())
} }
body := `{"display_name":"1","avatar_url":"","deactivated":false}`
if rec.Body.String() != body {
t.Fatalf("expected body %s, got %s", body, rec.Body.String())
}
})
t.Run("Retrieve non-existing account", func(t *testing.T) { if tc.Body != "" && tc.Body != rec.Body.String() {
req := test.NewRequest(t, http.MethodGet, "/_synapse/admin/v2/users/"+bob.ID) t.Fatalf("expected body %s, got %s", tc.Body, rec.Body.String())
req.Header.Set("Authorization", "Bearer "+adminToken)
rec := httptest.NewRecorder()
routers.SynapseAdmin.ServeHTTP(rec, req)
t.Logf("%s", rec.Body.String())
if rec.Code != http.StatusNotFound {
t.Fatalf("expected http status %d, got %d: %s", http.StatusNotFound, rec.Code, rec.Body.String())
} }
}) })
}
}) })
} }

View file

@ -31,9 +31,19 @@ var testIsBlacklistedOrBackingOff = func(s spec.ServerName) (*statistics.ServerS
return &statistics.ServerStatistics{}, nil return &statistics.ServerStatistics{}, nil
} }
type roundTripper struct{} type roundTripper struct {
roundTrip func(request *http.Request) (*http.Response, error)
}
func (rt *roundTripper) RoundTrip(request *http.Request) (*http.Response, error) { func (rt *roundTripper) RoundTrip(request *http.Request) (*http.Response, error) {
return rt.roundTrip(request)
}
func TestVerifyUserFromRequest(t *testing.T) {
aliceUser := test.NewUser(t, test.WithAccountType(uapi.AccountTypeUser))
bobUser := test.NewUser(t, test.WithAccountType(uapi.AccountTypeUser))
roundTrip := func(request *http.Request) (*http.Response, error) {
var ( var (
respBody string respBody string
statusCode int statusCode int
@ -53,7 +63,7 @@ func (rt *roundTripper) RoundTrip(request *http.Request) (*http.Response, error)
Active: true, Active: true,
Scope: "urn:matrix:org.matrix.msc2967.client:device:devAlice urn:matrix:org.matrix.msc2967.client:api:*", Scope: "urn:matrix:org.matrix.msc2967.client:device:devAlice urn:matrix:org.matrix.msc2967.client:api:*",
Sub: "111111111111111111", Sub: "111111111111111111",
Username: "1", Username: aliceUser.Localpart,
} }
b, _ := json.Marshal(resp) b, _ := json.Marshal(resp)
respBody = string(b) respBody = string(b)
@ -63,7 +73,7 @@ func (rt *roundTripper) RoundTrip(request *http.Request) (*http.Response, error)
Active: true, Active: true,
Scope: "urn:matrix:org.matrix.msc2967.client:device:devBob urn:matrix:org.matrix.msc2967.client:api:*", Scope: "urn:matrix:org.matrix.msc2967.client:device:devBob urn:matrix:org.matrix.msc2967.client:api:*",
Sub: "222222222222222222", Sub: "222222222222222222",
Username: "2", Username: bobUser.Localpart,
} }
b, _ := json.Marshal(resp) b, _ := json.Marshal(resp)
respBody = string(b) respBody = string(b)
@ -87,9 +97,8 @@ func (rt *roundTripper) RoundTrip(request *http.Request) (*http.Response, error)
return &resp, nil return &resp, nil
} }
func TestVerifyUserFromRequest(t *testing.T) {
httpClient := http.Client{ httpClient := http.Client{
Transport: &roundTripper{}, Transport: &roundTripper{roundTrip: roundTrip},
} }
ctx := context.Background() ctx := context.Background()
@ -123,9 +132,6 @@ func TestVerifyUserFromRequest(t *testing.T) {
} }
u, _ := url.Parse("https://example.com/something") u, _ := url.Parse("https://example.com/something")
aliceUser := test.NewUser(t, test.WithAccountType(uapi.AccountTypeUser))
bobUser := test.NewUser(t, test.WithAccountType(uapi.AccountTypeUser))
t.Run("existing user and active token", func(t *testing.T) { t.Run("existing user and active token", func(t *testing.T) {
localpart, serverName, _ := gomatrixserverlib.SplitID('@', aliceUser.ID) localpart, serverName, _ := gomatrixserverlib.SplitID('@', aliceUser.ID)
userRes := &uapi.PerformAccountCreationResponse{} userRes := &uapi.PerformAccountCreationResponse{}