@@ -11,8 +11,7 @@ import { throwErrorIfNotMod } from "shared/helpers/auth";
1111import * as constants from "common/envs/constants" ;
1212import * as supabaseUsers from "shared/supabase/users" ;
1313import * 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
1817describe ( '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} ) ;
0 commit comments