summaryrefslogtreecommitdiffstats
path: root/ui/src/api_tests/api.spec.ts
blob: 5db9ee64799ab84b218d696578758bf6a9c9386b (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
import fetch from 'node-fetch';

import {
  LoginForm,
  LoginResponse,
  GetPostsForm,
  GetPostsResponse,
  CommentForm,
  CommentResponse,
  ListingType,
  SortType,
} from '../interfaces';

let baseUrl = 'https://test.lemmy.ml';
let apiUrl = `${baseUrl}/api/v1`;
let auth: string;

beforeAll(async () => {
  console.log('Logging in as test user.');
  let form: LoginForm = {
    username_or_email: 'tester',
    password: 'tester',
  };

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

  auth = res.jwt;
});

test('Get test user posts', async () => {
  let form: GetPostsForm = {
    type_: ListingType[ListingType.All],
    sort: SortType[SortType.TopAll],
    auth,
  };

  let res: GetPostsResponse = await fetch(
    `${apiUrl}/post/list?type_=${form.type_}&sort=${form.sort}&auth=${auth}`
  ).then(d => d.json());

  // console.debug(res);

  expect(res.posts[0].id).toBe(2);
});

test('Create test comment', async () => {
  let content = 'A jest test comment';
  let form: CommentForm = {
    post_id: 2,
    content,
    auth,
  };

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

  expect(res.comment.content).toBe(content);
});

test('adds 1 + 2 to equal 3', () => {
  let sum = (a: number, b: number) => a + b;
  expect(sum(1, 2)).toBe(3);
});

test(`Get ${baseUrl} nodeinfo href`, async () => {
  let url = `${baseUrl}/.well-known/nodeinfo`;
  let href = `${baseUrl}/nodeinfo/2.0.json`;
  let res = await fetch(url).then(d => d.json());
  expect(res.links.href).toBe(href);
});

function wrapper(form: any): string {
  return JSON.stringify(form);
}