summaryrefslogtreecommitdiffstats
path: root/ui/src/api_tests/api.spec.ts
blob: 7a810d9a00d2b455dc52cb117325bba20a053ff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import fetch from 'node-fetch';

import {
  LoginForm,
  LoginResponse,
  PostForm,
  PostResponse,
  SearchResponse,
  FollowCommunityForm,
  CommunityResponse,
  GetFollowedCommunitiesResponse,
  GetPostForm,
  GetPostResponse,
  CommentForm,
  CommentResponse,
} from '../interfaces';

let lemmyAlphaUrl = 'http://localhost:8540';
let lemmyBetaUrl = 'http://localhost:8550';
let lemmyAlphaApiUrl = `${lemmyAlphaUrl}/api/v1`;
let lemmyBetaApiUrl = `${lemmyBetaUrl}/api/v1`;
let lemmyAlphaAuth: string;
let lemmyBetaAuth: string;

// Workaround for tests being run before beforeAll() is finished
// https://github.com/facebook/jest/issues/9527#issuecomment-592406108
describe('main', () => {
  beforeAll(async () => {
    console.log('Logging in as lemmy_alpha');
    let form: LoginForm = {
      username_or_email: 'lemmy_alpha',
      password: 'lemmy',
    };

    let res: LoginResponse = await fetch(`${lemmyAlphaApiUrl}/user/login`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: wrapper(form),
    }).then(d => d.json());

    lemmyAlphaAuth = res.jwt;

    console.log('Logging in as lemmy_beta');
    let formB = {
      username_or_email: 'lemmy_beta',
      password: 'lemmy',
    };

    let resB: LoginResponse = await fetch(`${lemmyBetaApiUrl}/user/login`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: wrapper(formB),
    }).then(d => d.json());

    lemmyBetaAuth = resB.jwt;
  });

  describe('beta_fetch', () => {
    test('Create test post on alpha and fetch it on beta', async () => {
      let name = 'A jest test post';
      let postForm: PostForm = {
        name,
        auth: lemmyAlphaAuth,
        community_id: 2,
        creator_id: 2,
        nsfw: false,
      };

      let createResponse: PostResponse = await fetch(
        `${lemmyAlphaApiUrl}/post`,
        {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: wrapper(postForm),
        }
      ).then(d => d.json());
      expect(createResponse.post.name).toBe(name);

      let searchUrl = `${lemmyBetaApiUrl}/search?q=${createResponse.post.ap_id}&type_=All&sort=TopAll`;
      let searchResponse: SearchResponse = await fetch(searchUrl, {
        method: 'GET',
      }).then(d => d.json());

      // TODO: check more fields
      expect(searchResponse.posts[0].name).toBe(name);
    });
  });

  describe('follow_accept', () => {
    test('/u/lemmy_alpha follows and accepts lemmy_beta/c/main', async () => {
      // Make sure lemmy_beta/c/main is cached on lemmy_alpha
      // Use short-hand search url
      let searchUrl = `${lemmyAlphaApiUrl}/search?q=/c/main@lemmy_beta:8550&type_=All&sort=TopAll`;

      let searchResponse: SearchResponse = await fetch(searchUrl, {
        method: 'GET',
      }).then(d => d.json());

      expect(searchResponse.communities[0].name).toBe('main');

      let followForm: FollowCommunityForm = {
        community_id: searchResponse.communities[0].id,
        follow: true,
        auth: lemmyAlphaAuth,
      };

      let followRes: CommunityResponse = await fetch(
        `${lemmyAlphaApiUrl}/community/follow`,
        {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: wrapper(followForm),
        }
      ).then(d => d.json());

      // Make sure the follow response went through
      expect(followRes.community.local).toBe(false);
      expect(followRes.community.name).toBe('main');

      // Check that you are subscribed to it locally
      let followedCommunitiesUrl = `${lemmyAlphaApiUrl}/user/followed_communities?&auth=${lemmyAlphaAuth}`;
      let followedCommunitiesRes: GetFollowedCommunitiesResponse = await fetch(
        followedCommunitiesUrl,
        {
          method: 'GET',
        }
      ).then(d => d.json());

      expect(followedCommunitiesRes.communities[1].community_local).toBe(false);
    });
  });

  describe('create test post', () => {
    test('/u/lemmy_alpha creates a post on /c/lemmy_beta/main, its on both instances', async () => {
      let name = 'A jest test federated post';
      let postForm: PostForm = {
        name,
        auth: lemmyAlphaAuth,
        community_id: 3,
        creator_id: 2,
        nsfw: false,
      };

      let createResponse: PostResponse = await fetch(
        `${lemmyAlphaApiUrl}/post`,
        {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: wrapper(postForm),
        }
      ).then(d => d.json());

      expect(createResponse.post.name).toBe(name);
      expect(createResponse.post.community_local).toBe(false);
      expect(createResponse.post.creator_local).toBe(true);
      expect(createResponse.post.score).toBe(1);

      let getPostUrl = `${lemmyBetaApiUrl}/post?id=2`;
      let getPostRes: GetPostResponse = await fetch(getPostUrl, {
        method: 'GET',
      }).then(d => d.json());

      expect(getPostRes.post.name).toBe(name);
      expect(getPostRes.post.community_local).toBe(true);
      expect(getPostRes.post.creator_local).toBe(false);
      expect(getPostRes.post.score).toBe(1);
    });
  });

  describe('update test post', () => {
    test('/u/lemmy_alpha updates a post on /c/lemmy_beta/main, the update is on both', async () => {
      let name = 'A jest test federated post, updated';
      let postForm: PostForm = {
        name,
        edit_id: 2,
        auth: lemmyAlphaAuth,
        community_id: 3,
        creator_id: 2,
        nsfw: false,
      };

      let updateResponse: PostResponse = await fetch(
        `${lemmyAlphaApiUrl}/post`,
        {
          method: 'PUT',
          headers: {
            'Content-Type': 'application/json',
          },
          body: wrapper(postForm),
        }
      ).then(d => d.json());

      expect(updateResponse.post.name).toBe(name);
      expect(updateResponse.post.community_local).toBe(false);
      expect(updateResponse.post.creator_local).toBe(true);

      let getPostUrl = `${lemmyBetaApiUrl}/post?id=2`;
      let getPostRes: GetPostResponse = await fetch(getPostUrl, {
        method: 'GET',
      }).then(d => d.json());

      expect(getPostRes.post.name).toBe(name);
      expect(getPostRes.post.community_local).toBe(true);
      expect(getPostRes.post.creator_local).toBe(false);
    });
  });

  describe('create test comment', () => {
    test('/u/lemmy_alpha creates a comment on /c/lemmy_beta/main, its on both instances', async () => {
      let content = 'A jest test federated comment';
      let commentForm: CommentForm = {
        content,
        post_id: 2,
        auth: lemmyAlphaAuth,
      };

      let createResponse: CommentResponse = await fetch(
        `${lemmyAlphaApiUrl}/comment`,
        {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          },
          body: wrapper(commentForm),
        }
      ).then(d => d.json());

      expect(createResponse.comment.content).toBe(content);
      expect(createResponse.comment.community_local).toBe(false);
      expect(createResponse.comment.creator_local).toBe(true);
      expect(createResponse.comment.score).toBe(1);

      let getPostUrl = `${lemmyBetaApiUrl}/post?id=2`;