summaryrefslogtreecommitdiffstats
path: root/ui/src/api_tests
diff options
context:
space:
mode:
authorDessalines <tyhou13@gmx.com>2020-04-27 12:57:00 -0400
committerDessalines <tyhou13@gmx.com>2020-04-27 12:57:00 -0400
commit22abbebd41d586298c62bb6a45efa7a96d998049 (patch)
tree60c31f49e9cee360cd06edc18eb53f93e953d6b7 /ui/src/api_tests
parent3ce061836242813730ec0b63240097347647dfc4 (diff)
Lots of additions to federation.
- Added a shared inbox. - Added federated comments, comment updates, and tests. - Abstracted ap object sends into a common trait.
Diffstat (limited to 'ui/src/api_tests')
-rw-r--r--ui/src/api_tests/api.spec.ts300
1 files changed, 279 insertions, 21 deletions
diff --git a/ui/src/api_tests/api.spec.ts b/ui/src/api_tests/api.spec.ts
index 07e12ecf..49fd0878 100644
--- a/ui/src/api_tests/api.spec.ts
+++ b/ui/src/api_tests/api.spec.ts
@@ -6,6 +6,13 @@ import {
PostForm,
PostResponse,
SearchResponse,
+ FollowCommunityForm,
+ CommunityResponse,
+ GetFollowedCommunitiesResponse,
+ GetPostForm,
+ GetPostResponse,
+ CommentForm,
+ CommentResponse,
} from '../interfaces';
let lemmyAlphaUrl = 'http://localhost:8540';
@@ -13,6 +20,7 @@ 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
@@ -33,37 +41,287 @@ describe('main', () => {
}).then(d => d.json());
lemmyAlphaAuth = res.jwt;
- });
- 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.log('Logging in as lemmy_beta');
+ let formB = {
+ username_or_email: 'lemmy_beta',
+ password: 'lemmy',
};
- let createResponse: PostResponse = await fetch(`${lemmyAlphaApiUrl}/post`, {
+ let resB: LoginResponse = await fetch(`${lemmyBetaApiUrl}/user/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
- body: wrapper(postForm),
+ body: wrapper(formB),
}).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());
+ 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');
+
+ // TODO
+ // Unfortunately the search is correctly
+ 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);
+
+ 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('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);
+
+ let getPostUrl = `${lemmyBetaApiUrl}/post?id=2`;
+ let getPostRes: GetPostResponse = await fetch(getPostUrl, {
+ method: 'GET',
+ }).then(d => d.json());
- // TODO: check more fields
- expect(searchResponse.posts[0].name).toBe(name);
+ expect(getPostRes.comments[0].content).toBe(content);
+ expect(getPostRes.comments[0].community_local).toBe(true);
+ expect(getPostRes.comments[0].creator_local).toBe(false);
+
+ // Now do beta replying to that comment, as a child comment
+ let contentBeta = 'A child federated comment from beta';
+ let commentFormBeta: CommentForm = {
+ content: contentBeta,
+ post_id: getPostRes.post.id,
+ parent_id: getPostRes.comments[0].id,
+ auth: lemmyBetaAuth,
+ };
+
+ let createResponseBeta: CommentResponse = await fetch(
+ `${lemmyBetaApiUrl}/comment`,
+ {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: wrapper(commentFormBeta),
+ }
+ ).then(d => d.json());
+
+ expect(createResponseBeta.comment.content).toBe(contentBeta);
+ expect(createResponseBeta.comment.community_local).toBe(true);
+ expect(createResponseBeta.comment.creator_local).toBe(true);
+ expect(createResponseBeta.comment.parent_id).toBe(1);
+
+ // Make sure lemmy alpha sees that new child comment from beta
+ let getPostUrlAlpha = `${lemmyAlphaApiUrl}/post?id=2`;
+ let getPostResAlpha: GetPostResponse = await fetch(getPostUrlAlpha, {
+ method: 'GET',
+ }).then(d => d.json());
+
+ // The newest show up first
+ expect(getPostResAlpha.comments[0].content).toBe(contentBeta);
+ expect(getPostResAlpha.comments[0].community_local).toBe(false);
+ expect(getPostResAlpha.comments[0].creator_local).toBe(false);
+ });
});
- function wrapper(form: any): string {
- return JSON.stringify(form);
- }
+ describe('update test comment', () => {
+ test('/u/lemmy_alpha updates a comment on /c/lemmy_beta/main, its on both instances', async () => {
+ let content = 'A jest test federated comment update';
+ let commentForm: CommentForm = {
+ content,
+ post_id: 2,
+ edit_id: 1,
+ auth: lemmyAlphaAuth,
+ creator_id: 2,
+ };
+
+ let updateResponse: CommentResponse = await fetch(
+ `${lemmyAlphaApiUrl}/comment`,
+ {
+ method: 'PUT',
+ headers: {
+ 'Content-Type': 'application/json',
+ },
+ body: wrapper(commentForm),
+ }
+ ).then(d => d.json());
+
+ expect(updateResponse.comment.content).toBe(content);
+ expect(updateResponse.comment.community_local).toBe(false);
+ expect(updateResponse.comment.creator_local).toBe(true);
+
+ let getPostUrl = `${lemmyBetaApiUrl}/post?id=2`;
+ let getPostRes: GetPostResponse = await fetch(getPostUrl, {
+ method: 'GET',
+ }).then(d => d.json());
+
+ expect(getPostRes.comments[1].content).toBe(content);
+ expect(getPostRes.comments[1].community_local).toBe(true);
+ expect(getPostRes.comments[1].creator_local).toBe(false);
+ });
+ });
});
+
+function wrapper(form: any): string {
+ return JSON.stringify(form);
+}