summaryrefslogtreecommitdiffstats
path: root/ui/src/api_tests
diff options
context:
space:
mode:
authorFelix <me@nutomic.com>2020-04-24 18:30:31 +0200
committerFelix <me@nutomic.com>2020-04-24 18:30:31 +0200
commitc3ac1649f2d982c29da3360d39c047ea6bdd9aa2 (patch)
treea057a7c7034758d3fd0d7de76202d698c498e845 /ui/src/api_tests
parente5497edd5c12b1355c82b224111bf7ce5bb1fd1e (diff)
Implement integration test for federation
Diffstat (limited to 'ui/src/api_tests')
-rw-r--r--ui/src/api_tests/api.spec.ts122
1 files changed, 53 insertions, 69 deletions
diff --git a/ui/src/api_tests/api.spec.ts b/ui/src/api_tests/api.spec.ts
index 5db9ee64..07e12ecf 100644
--- a/ui/src/api_tests/api.spec.ts
+++ b/ui/src/api_tests/api.spec.ts
@@ -3,83 +3,67 @@ import fetch from 'node-fetch';
import {
LoginForm,
LoginResponse,
- GetPostsForm,
- GetPostsResponse,
- CommentForm,
- CommentResponse,
- ListingType,
- SortType,
+ PostForm,
+ PostResponse,
+ SearchResponse,
} from '../interfaces';
-let baseUrl = 'https://test.lemmy.ml';
-let apiUrl = `${baseUrl}/api/v1`;
-let auth: string;
+let lemmyAlphaUrl = 'http://localhost:8540';
+let lemmyBetaUrl = 'http://localhost:8550';
+let lemmyAlphaApiUrl = `${lemmyAlphaUrl}/api/v1`;
+let lemmyBetaApiUrl = `${lemmyBetaUrl}/api/v1`;
+let lemmyAlphaAuth: string;
-beforeAll(async () => {
- console.log('Logging in as test user.');
- let form: LoginForm = {
- username_or_email: 'tester',
- password: 'tester',
- };
+// 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(`${apiUrl}/user/login`, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: wrapper(form),
- }).then(d => d.json());
+ let res: LoginResponse = await fetch(`${lemmyAlphaApiUrl}/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,
- };
+ lemmyAlphaAuth = res.jwt;
+ });
- let res: GetPostsResponse = await fetch(
- `${apiUrl}/post/list?type_=${form.type_}&sort=${form.sort}&auth=${auth}`
- ).then(d => d.json());
+ 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,
+ };
- // console.debug(res);
+ 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(res.posts[0].id).toBe(2);
-});
+ 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());
-test('Create test comment', async () => {
- let content = 'A jest test comment';
- let form: CommentForm = {
- post_id: 2,
- content,
- auth,
- };
+ // TODO: check more fields
+ expect(searchResponse.posts[0].name).toBe(name);
+ });
- 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);
+ function wrapper(form: any): string {
+ return JSON.stringify(form);
+ }
});
-
-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);
-}