Skip to content

Commit 26d9851

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents fbfb959 + 449b32d commit 26d9851

47 files changed

Lines changed: 3197 additions & 828 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

backend/api/tests/unit/ban-user.unit.test.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ import { throwErrorIfNotMod } from "shared/helpers/auth";
1111
import * as constants from "common/envs/constants";
1212
import * as supabaseUsers from "shared/supabase/users";
1313
import * as sharedAnalytics from "shared/analytics";
14-
import { } from "shared/helpers/auth";
15-
import { APIError, AuthedUser } from "api/helpers/endpoint"
14+
import { AuthedUser } from "api/helpers/endpoint"
1615

1716

1817
describe('banUser', () => {
@@ -24,13 +23,13 @@ describe('banUser', () => {
2423
(supabaseInit.createSupabaseDirectClient as jest.Mock)
2524
.mockReturnValue(mockPg);
2625
});
27-
2826
afterEach(() => {
2927
jest.restoreAllMocks();
3028
});
3129

32-
describe('should', () => {
33-
it('ban a user successfully', async () => {
30+
31+
describe('when given valid input', () => {
32+
it('should ban a user successfully', async () => {
3433
const mockUser = {
3534
userId: '123',
3635
unban: false
@@ -42,15 +41,25 @@ describe('banUser', () => {
4241

4342
await banUser(mockUser, mockAuth, mockReq);
4443

44+
expect(throwErrorIfNotMod).toBeCalledTimes(1);
4545
expect(throwErrorIfNotMod).toBeCalledWith(mockAuth.uid);
46+
expect(constants.isAdminId).toBeCalledTimes(1);
4647
expect(constants.isAdminId).toBeCalledWith(mockUser.userId);
47-
expect(sharedAnalytics.trackPublicEvent)
48-
.toBeCalledWith(mockAuth.uid, 'ban user', {userId: mockUser.userId});
49-
expect(supabaseUsers.updateUser)
50-
.toBeCalledWith(mockPg, mockUser.userId, {isBannedFromPosting: true});
48+
expect(sharedAnalytics.trackPublicEvent).toBeCalledTimes(1);
49+
expect(sharedAnalytics.trackPublicEvent).toBeCalledWith(
50+
mockAuth.uid,
51+
'ban user',
52+
{userId: mockUser.userId}
53+
);
54+
expect(supabaseUsers.updateUser).toBeCalledTimes(1);
55+
expect(supabaseUsers.updateUser).toBeCalledWith(
56+
mockPg,
57+
mockUser.userId,
58+
{isBannedFromPosting: true}
59+
);
5160
});
5261

53-
it('unban a user successfully', async () => {
62+
it('should unban a user successfully', async () => {
5463
const mockUser = {
5564
userId: '123',
5665
unban: true
@@ -64,13 +73,20 @@ describe('banUser', () => {
6473

6574
expect(throwErrorIfNotMod).toBeCalledWith(mockAuth.uid);
6675
expect(constants.isAdminId).toBeCalledWith(mockUser.userId);
67-
expect(sharedAnalytics.trackPublicEvent)
68-
.toBeCalledWith(mockAuth.uid, 'ban user', {userId: mockUser.userId});
69-
expect(supabaseUsers.updateUser)
70-
.toBeCalledWith(mockPg, mockUser.userId, {isBannedFromPosting: false});
76+
expect(sharedAnalytics.trackPublicEvent).toBeCalledWith(
77+
mockAuth.uid,
78+
'ban user',
79+
{userId: mockUser.userId}
80+
);
81+
expect(supabaseUsers.updateUser).toBeCalledWith(
82+
mockPg,
83+
mockUser.userId,
84+
{isBannedFromPosting: false}
85+
);
7186
});
72-
73-
it('throw and error if the ban requester is not a mod or admin', async () => {
87+
});
88+
describe('when an error occurs', () => {
89+
it('throw if the ban requester is not a mod or admin', async () => {
7490
const mockUser = {
7591
userId: '123',
7692
unban: false
@@ -79,21 +95,16 @@ describe('banUser', () => {
7995
const mockReq = {} as any;
8096

8197
(throwErrorIfNotMod as jest.Mock).mockRejectedValue(
82-
new APIError(
83-
403,
84-
`User ${mockAuth.uid} must be an admin or trusted to perform this action.`
85-
)
98+
new Error(`User ${mockAuth.uid} must be an admin or trusted to perform this action.`)
8699
);
87100

88101
await expect(banUser(mockUser, mockAuth, mockReq))
89102
.rejects
90103
.toThrowError(`User ${mockAuth.uid} must be an admin or trusted to perform this action.`);
91104
expect(throwErrorIfNotMod).toBeCalledWith(mockAuth.uid);
92-
expect(sharedAnalytics.trackPublicEvent).toBeCalledTimes(0);
93-
expect(supabaseUsers.updateUser).toBeCalledTimes(0);
94105
});
95106

96-
it('throw an error if the ban target is an admin', async () => {
107+
it('throw if the ban target is an admin', async () => {
97108
const mockUser = {
98109
userId: '123',
99110
unban: false
@@ -108,8 +119,6 @@ describe('banUser', () => {
108119
.toThrowError('Cannot ban admin');
109120
expect(throwErrorIfNotMod).toBeCalledWith(mockAuth.uid);
110121
expect(constants.isAdminId).toBeCalledWith(mockUser.userId);
111-
expect(sharedAnalytics.trackPublicEvent).toBeCalledTimes(0);
112-
expect(supabaseUsers.updateUser).toBeCalledTimes(0);
113122
});
114123
});
115124
});

backend/api/tests/unit/block-user.unit.test.ts

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,45 @@ describe('blockUser', () => {
2323
(supabaseInit.createSupabaseDirectClient as jest.Mock)
2424
.mockReturnValue(mockPg)
2525
});
26-
2726
afterEach(() => {
2827
jest.restoreAllMocks();
2928
});
3029

31-
describe('should', () => {
30+
describe('when given valid input', () => {
3231
it('block the user successfully', async () => {
3332
const mockParams = { id: '123' }
3433
const mockAuth = {uid: '321'} as AuthedUser;
3534
const mockReq = {} as any;
3635

37-
(supabaseUsers.updatePrivateUser as jest.Mock).mockResolvedValue(null);
38-
3936
await blockUserModule.blockUser(mockParams, mockAuth, mockReq)
4037

41-
expect(mockPg.tx).toHaveBeenCalledTimes(1)
42-
43-
expect(supabaseUsers.updatePrivateUser)
44-
.toHaveBeenCalledWith(
45-
expect.any(Object),
46-
mockAuth.uid,
47-
{ blockedByUserIds: supabaseUtils.FieldVal.arrayConcat(mockParams.id)}
48-
);
49-
expect(supabaseUsers.updatePrivateUser)
50-
.toHaveBeenCalledWith(
51-
expect.any(Object),
52-
mockParams.id,
53-
{ blockedByUserIds: supabaseUtils.FieldVal.arrayConcat(mockAuth.uid)}
54-
);
38+
expect(mockPg.tx).toHaveBeenCalledTimes(1);
39+
expect(supabaseUsers.updatePrivateUser).toBeCalledTimes(2);
40+
expect(supabaseUsers.updatePrivateUser).toHaveBeenNthCalledWith(
41+
1,
42+
expect.any(Object),
43+
mockAuth.uid,
44+
{ blockedByUserIds: supabaseUtils.FieldVal.arrayConcat(mockParams.id)}
45+
);
46+
expect(supabaseUsers.updatePrivateUser).toHaveBeenNthCalledWith(
47+
2,
48+
expect.any(Object),
49+
mockParams.id,
50+
{ blockedByUserIds: supabaseUtils.FieldVal.arrayConcat(mockAuth.uid)}
51+
);
5552
});
56-
53+
});
54+
describe('when an error occurs', () => {
5755
it('throw an error if the user tries to block themselves', async () => {
5856
const mockParams = { id: '123' }
5957
const mockAuth = {uid: '123'} as AuthedUser;
6058
const mockReq = {} as any;
6159

6260
expect(blockUserModule.blockUser(mockParams, mockAuth, mockReq))
6361
.rejects
64-
.toThrowError('You cannot block yourself')
65-
66-
expect(mockPg.tx).toHaveBeenCalledTimes(0)
62+
.toThrowError('You cannot block yourself');
6763
});
6864
});
69-
7065
});
7166

7267
describe('unblockUser', () => {
@@ -84,35 +79,32 @@ describe('unblockUser', () => {
8479
(supabaseInit.createSupabaseDirectClient as jest.Mock)
8580
.mockReturnValue(mockPg)
8681
});
87-
8882
afterEach(() => {
8983
jest.restoreAllMocks();
9084
});
9185

92-
describe('should', () => {
93-
it('block the user successfully', async () => {
86+
describe('when given valid input', () => {
87+
it('should block the user successfully', async () => {
9488
const mockParams = { id: '123' }
9589
const mockAuth = {uid: '321'} as AuthedUser;
9690
const mockReq = {} as any;
9791

98-
(supabaseUsers.updatePrivateUser as jest.Mock).mockResolvedValue(null);
99-
10092
await blockUserModule.unblockUser(mockParams, mockAuth, mockReq)
10193

102-
expect(mockPg.tx).toHaveBeenCalledTimes(1)
103-
104-
expect(supabaseUsers.updatePrivateUser)
105-
.toHaveBeenCalledWith(
106-
expect.any(Object),
107-
mockAuth.uid,
108-
{ blockedByUserIds: supabaseUtils.FieldVal.arrayConcat(mockParams.id)}
109-
);
110-
expect(supabaseUsers.updatePrivateUser)
111-
.toHaveBeenCalledWith(
112-
expect.any(Object),
113-
mockParams.id,
114-
{ blockedByUserIds: supabaseUtils.FieldVal.arrayConcat(mockAuth.uid)}
115-
);
94+
expect(mockPg.tx).toHaveBeenCalledTimes(1);
95+
expect(supabaseUsers.updatePrivateUser).toBeCalledTimes(2);
96+
expect(supabaseUsers.updatePrivateUser).toHaveBeenNthCalledWith(
97+
1,
98+
expect.any(Object),
99+
mockAuth.uid,
100+
{ blockedByUserIds: supabaseUtils.FieldVal.arrayConcat(mockParams.id)}
101+
);
102+
expect(supabaseUsers.updatePrivateUser).toHaveBeenNthCalledWith(
103+
2,
104+
expect.any(Object),
105+
mockParams.id,
106+
{ blockedByUserIds: supabaseUtils.FieldVal.arrayConcat(mockAuth.uid)}
107+
);
116108
});
117109
});
118110

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
1-
import * as supabaseInit from "shared/supabase/init";
1+
jest.mock('shared/supabase/init');
2+
23
import {getCompatibleProfiles} from "api/compatible-profiles";
4+
import * as supabaseInit from "shared/supabase/init";
35

4-
jest.mock('shared/supabase/init')
56

67
describe('getCompatibleProfiles', () => {
8+
let mockPg = {} as any;
79
beforeEach(() => {
810
jest.resetAllMocks();
9-
const mockPg = {
10-
none: jest.fn().mockResolvedValue(null),
11-
one: jest.fn().mockResolvedValue(null),
12-
oneOrNone: jest.fn().mockResolvedValue(null),
13-
any: jest.fn().mockResolvedValue([]),
14-
map: jest.fn().mockResolvedValue([["abc", {score: 0.69}]]),
15-
} as any;
11+
mockPg = {
12+
map: jest.fn().mockResolvedValue([]),
13+
};
1614
(supabaseInit.createSupabaseDirectClient as jest.Mock)
1715
.mockReturnValue(mockPg);
1816
});
19-
2017
afterEach(() => {
2118
jest.restoreAllMocks();
2219
});
2320

24-
describe('should', () => {
25-
it('successfully get compatible profiles when supplied with a valid user Id', async () => {
26-
const results = await getCompatibleProfiles("123");
21+
describe('when given valid input', () => {
22+
it('should successfully get compatible profiles', async () => {
23+
const mockProps = '123';
24+
const mockScores = ["abc", { score: 0.69 }];
25+
const mockScoresFromEntries = {"abc": { score: 0.69 }};
26+
27+
(mockPg.map as jest.Mock).mockResolvedValue([mockScores]);
28+
29+
const results = await getCompatibleProfiles(mockProps);
30+
const [sql, param, fn] = mockPg.map.mock.calls[0];
31+
2732
expect(results.status).toEqual('success');
28-
expect(results.profileCompatibilityScores).toEqual({"abc": {score: 0.69}});
33+
expect(results.profileCompatibilityScores).toEqual(mockScoresFromEntries);
34+
expect(mockPg.map).toBeCalledTimes(1);
35+
expect(sql).toContain('select *');
36+
expect(sql).toContain('from compatibility_scores');
37+
expect(param).toStrictEqual([mockProps]);
38+
expect(fn).toEqual(expect.any(Function));
2939
});
30-
3140
});
3241
});

0 commit comments

Comments
 (0)