import fetch from 'node-fetch';
import {
LoginForm,
LoginResponse,
PostForm,
PostResponse,
SearchResponse,
FollowCommunityForm,
CommunityResponse,
GetFollowedCommunitiesResponse,
GetPostForm,
GetPostResponse,
CommentForm,
CommentResponse,
CommunityForm,
GetCommunityForm,
GetCommunityResponse,
} 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
let searchUrl = `${lemmyAlphaApiUrl}/search?q=http://lemmy_beta:8550/c/main&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: