MCPcopy Create free account
hub / github.com/daodst/chat / TestQueryProfile

Function TestQueryProfile

userapi/userapi_test.go:72–148  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

70}
71
72func TestQueryProfile(t *testing.T) {
73 aliceAvatarURL := "mxc://example.com/alice"
74 aliceDisplayName := "Alice"
75 // only one DBType, since userapi.AddInternalRoutes complains about multiple prometheus counters added
76 userAPI, accountDB, close := MustMakeInternalAPI(t, apiTestOpts{}, test.DBTypeSQLite)
77 defer close()
78 _, err := accountDB.CreateAccount(context.TODO(), "alice", "foobar", "", api.AccountTypeUser)
79 if err != nil {
80 t.Fatalf("failed to make account: %s", err)
81 }
82 if err := accountDB.SetAvatarURL(context.TODO(), "alice", aliceAvatarURL); err != nil {
83 t.Fatalf("failed to set avatar url: %s", err)
84 }
85 if err := accountDB.SetDisplayName(context.TODO(), "alice", aliceDisplayName); err != nil {
86 t.Fatalf("failed to set display name: %s", err)
87 }
88
89 testCases := []struct {
90 req api.QueryProfileRequest
91 wantRes api.QueryProfileResponse
92 wantErr error
93 }{
94 {
95 req: api.QueryProfileRequest{
96 UserID: fmt.Sprintf("@alice:%s", serverName),
97 },
98 wantRes: api.QueryProfileResponse{
99 UserExists: true,
100 AvatarURL: aliceAvatarURL,
101 DisplayName: aliceDisplayName,
102 },
103 },
104 {
105 req: api.QueryProfileRequest{
106 UserID: fmt.Sprintf("@bob:%s", serverName),
107 },
108 wantRes: api.QueryProfileResponse{
109 UserExists: false,
110 },
111 },
112 {
113 req: api.QueryProfileRequest{
114 UserID: "@alice:wrongdomain.com",
115 },
116 wantErr: fmt.Errorf("wrong domain"),
117 },
118 }
119
120 runCases := func(testAPI api.UserInternalAPI) {
121 for _, tc := range testCases {
122 var gotRes api.QueryProfileResponse
123 gotErr := testAPI.QueryProfile(context.TODO(), &tc.req, &gotRes)
124 if tc.wantErr == nil && gotErr != nil || tc.wantErr != nil && gotErr == nil {
125 t.Errorf("QueryProfile error, got %s want %s", gotErr, tc.wantErr)
126 continue
127 }
128 if !reflect.DeepEqual(tc.wantRes, gotRes) {
129 t.Errorf("QueryProfile response got %+v want %+v", gotRes, tc.wantRes)

Callers

nothing calls this directly

Calls 6

MustMakeInternalAPIFunction · 0.85
RunMethod · 0.80
ListenAndServeMethod · 0.80
SetAvatarURLMethod · 0.65
SetDisplayNameMethod · 0.65
QueryProfileMethod · 0.65

Tested by

no test coverage detected