forgejo-notifications-core/src/api.test.ts

78 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-09-18 20:43:58 +05:30
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
import Forgejo from "./index";
2023-09-18 22:30:02 +05:30
import authtoken from "../tmp/user1-accesstoken.json";
2023-09-18 17:56:40 +05:30
const forgeoUrl = authtoken["forgejo_url"];
2023-09-18 19:06:17 +05:30
const token = authtoken["sha1"];
const username = authtoken["login"];
const repo = authtoken["repo"];
const api = new Forgejo(forgeoUrl, "owner_user");
api.setTokenAuth(token);
2023-09-18 19:06:17 +05:30
2023-09-18 17:56:40 +05:30
test("use authentication without setting it ", () => {
const api = new Forgejo(forgeoUrl, "owner_user");
2023-09-18 17:56:40 +05:30
expect(() => api.getTokenAuth()).toThrow();
2023-09-18 19:06:17 +05:30
api.setTokenAuth(token);
expect(api.getTokenAuth().getToken()).toBe(token);
let headers = api.getTokenAuthHeader();
expect(headers.Authorization).toBe(`token ${token}`);
});
test("verify /user API ", async () => {
let user = await api.getUser();
expect(user["login"]).toBe(username);
2023-09-18 17:56:40 +05:30
});
test("notifications API ", async () => {
let notifications = await api.getNotifications();
expect(notifications.length).toBe(5);
expect(await api.getNumUnreadNotifications()).toBe(5);
let notification = await api.getNotificationThread(notifications[0].id);
expect(notification).toEqual(notifications[0]);
});
test("issue API ", async () => {
let notifications = await api.getNotifications();
let issue = await api.getIssue(username, repo, 1);
2023-09-19 00:01:50 +05:30
let notification = notifications.find((n) => {
if (n.subject.html_url == issue.html_url) {
return n;
}
});
2023-09-19 01:33:45 +05:30
expect(notification.subject.type).toEqual("Issue");
expect(issue.title).toEqual(notification.subject.title);
expect(issue.lazy_comments_data).toBeUndefined();
expect(issue.created_at instanceof Date).toBe(true);
expect(issue.updated_at instanceof Date).toBe(true);
if (issue.closed_at) {
// TODO: add issue closing functionality in integration/forgejo.py and verify this
expect(issue.closed_at instanceof Date).toBe(true);
}
// expect(typeof(issue.created_at)).toBe('Date');
});
test("comments API ", async () => {
let notifications = await api.getNotifications();
let issue = await api.getIssue(username, repo, 1);
issue = await api.getCommentsForIssue(issue);
expect(issue.lazy_comments_data.length).toBe(2);
issue.lazy_comments_data?.forEach((comment) => {
expect(comment.created_at instanceof Date).toBe(true);
expect(comment.updated_at instanceof Date).toBe(true);
});
});
test("mentions in issue", async () => {
expect(await api.findMentionsInIssue(username, repo, 1)).toBe(true);
expect(await api.findMentionsInIssue(username, repo, 2)).toBe(true);
expect(await api.findMentionsInIssue(username, repo, 3)).toBe(true);
expect(await api.findMentionsInIssue(username, repo, 4)).toBe(true);
expect(await api.findMentionsInIssue(username, repo, 5)).toBe(false);
});