Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
user testing
Browse files Browse the repository at this point in the history
  • Loading branch information
williamhorning committed Jul 7, 2024
1 parent dee3a08 commit d1a0e5d
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/internal/post.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from 'jsr:@std/[email protected]';
import { assertEquals } from './deps.ts';
import {
type api_post,
type post,
Expand Down
34 changes: 34 additions & 0 deletions tests/internal/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { assertEquals } from './deps.ts';
import type { api_user, user } from '../../src/interfaces/user.ts';

export function user_is_api_user(user: user, api_user: api_user) {
assertEquals(user.id, api_user._id);
assertEquals(user.username, api_user.lower_username);
assertEquals(user.avatar, api_user.avatar);
assertEquals(user.banned, api_user.banned);
assertEquals(user.created, api_user.created);
assertEquals(user.flags, api_user.flags);
assertEquals(user.last_seen, api_user.last_seen);
assertEquals(user.lvl, api_user.lvl);
assertEquals(user.permissions, api_user.permissions);
assertEquals(user.pfp_data, api_user.pfp_data);
assertEquals(user.profile_color, api_user.avatar_color);
assertEquals(user.quote, api_user.quote);
assertEquals(user.uuid, api_user.uuid);
}

export const regular_user: api_user = {
_id: 'test',
avatar: 'test',
avatar_color: 'test',
banned: false,
created: 0,
flags: 0,
last_seen: 0,
lower_username: 'test',
lvl: 0,
permissions: 0,
pfp_data: 0,
quote: 'test',
uuid: 'test',
};
140 changes: 140 additions & 0 deletions tests/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import {
is_api_user,
user,
user_relationship_status,
} from '../src/interfaces/user.ts';
import {
assertEquals,
assertRejects,
assertThrows,
mockFetch,
} from './internal/deps.ts';
import { post_is_api_post } from './internal/post.ts';
import { regular_user, user_is_api_user } from './internal/user.ts';
import { regular_post } from './internal/post.ts';

Deno.test('api_user validation', async (i) => {
await i.step('number (invalid)', () => {
assertEquals(is_api_user(1), false);
});

await i.step('string (invalid)', () => {
assertEquals(is_api_user('test'), false);
});

await i.step('empty object (invalid)', () => {
assertEquals(is_api_user({}), false);
});

await i.step('user (valid)', () => {
assertEquals(is_api_user(regular_user), true);
});
});

Deno.test('user construction', async (i) => {
await i.step('throw error if data is not a user', () => {
assertThrows(() => {
new user({
api_url: 'http://localhost:8000',
api_token: 'test',
// @ts-ignore: intentionally passing an empty object
data: {},
});
});
});

await i.step('construct valid user', () => {
const u = new user({
api_url: 'http://localhost:8000',
api_token: 'test',
data: regular_user,
});

user_is_api_user(u, regular_user);
});
});

Deno.test('user reporting', async (i) => {
const u = new user({
api_url: 'http://localhost:8000',
api_token: 'test',
data: regular_user,
});

await i.step('report (successful)', async () => {
mockFetch('http://localhost:8000/users/:id/report', {
status: 200,
});

await u.report({ comment: 'test', reason: 'test' });
});

await i.step('report (failed)', async () => {
mockFetch('http://localhost:8000/users/:id/report', {
status: 400,
});

await assertRejects(async () => {
await u.report({ comment: 'test', reason: 'test' });
});
});
});

Deno.test('user relationship', async (i) => {
const u = new user({
api_url: 'http://localhost:8000',
api_token: 'test',
data: regular_user,
});

await i.step('change_relationship (successful)', async () => {
mockFetch('http://localhost:8000/users/:id/relationship', {
status: 200,
});

await u.change_relationship(user_relationship_status.blocked);
});

await i.step('change_relationship (failed)', async () => {
mockFetch('http://localhost:8000/users/:id/relationship', {
status: 404,
body: JSON.stringify({ error: true, type: 'notFound' }),
});

await assertRejects(async () => {
await u.change_relationship(user_relationship_status.blocked);
});
});
});

Deno.test('user posts', async (i) => {
const u = new user({
api_url: 'http://localhost:8000',
api_token: 'test',
data: regular_user,
});

await i.step('get_posts (successful)', async () => {
mockFetch('http://localhost:8000/users/:id/posts', {
status: 200,
body: JSON.stringify({ autoget: [regular_post] }),
});

const posts = await u.get_posts();

assertEquals(posts.length, 1);

post_is_api_post(posts[0], regular_post);
});

await i.step('get_posts (failed)', async () => {
mockFetch('http://localhost:8000/users/:id/posts', {
status: 404,
body: JSON.stringify({ error: true, type: 'notFound' }),
});

await assertRejects(async () => {
await u.get_posts();
});
});
});

0 comments on commit d1a0e5d

Please sign in to comment.