1+ import { BugSplatApiClient } from '@common' ;
2+ import { config } from '@spec/config' ;
3+ import { UserApiResponseStatus , UsersApiClient } from './users-api-client' ;
4+
5+ describe ( 'UsersApiClient' , ( ) => {
6+ let companyId : number ;
7+ let testEmail : string ;
8+ let usersClient : UsersApiClient ;
9+
10+ beforeEach ( async ( ) => {
11+ const { host, email, password } = config ;
12+ const bugsplat = await BugSplatApiClient . createAuthenticatedClientForNode ( email , password , host ) ;
13+ companyId = await getCompanyId ( bugsplat , config . database ) ;
14+ usersClient = new UsersApiClient ( bugsplat ) ;
15+ testEmail = 'bobby+unittests@bugsplat.com' ;
16+ } ) ;
17+
18+ describe ( 'getUsers' , ( ) => {
19+ it ( 'should return 200 and a specific user' , async ( ) => {
20+ const database = config . database ;
21+
22+ const { rows } = await usersClient . getUsers ( {
23+ database,
24+ email : config . email
25+ } ) ;
26+
27+ const userRow = rows [ 0 ] ;
28+ expect ( rows ) . toBeTruthy ( ) ;
29+ expect ( userRow ) . toBeTruthy ( ) ;
30+ } ) ;
31+
32+ it ( 'should return 200 and array of users' , async ( ) => {
33+ const database = config . database ;
34+
35+ const { rows } = await usersClient . getUsers ( {
36+ database,
37+ } ) ;
38+
39+ const userRow = rows . find ( row => row . username === config . email ) ;
40+ expect ( rows ) . toBeTruthy ( ) ;
41+ expect ( userRow ) . toBeTruthy ( ) ;
42+ } ) ;
43+ } ) ;
44+
45+ describe ( 'addUserToDatabase' , ( ) => {
46+ it ( 'should return 200 and message' , async ( ) => {
47+ const response = await usersClient . addUserToDatabase ( config . database , testEmail ) ;
48+ const body = await response . json ( ) ;
49+ expect ( response . status ) . toEqual ( 200 ) ;
50+ expect ( body . status ) . toEqual ( UserApiResponseStatus . success ) ;
51+ } ) ;
52+ } ) ;
53+
54+ describe ( 'removeUserFromDatabase' , ( ) => {
55+ it ( 'should return 200 and message' , async ( ) => {
56+ const { uId } = await await usersClient . addUserToDatabase ( config . database , testEmail ) . then ( response => response . json ( ) ) ;
57+ const response = await usersClient . removeUserFromDatabase ( config . database , uId ) ;
58+ const body = await response . json ( ) ;
59+ expect ( response . status ) . toEqual ( 200 ) ;
60+ expect ( body . status ) . toEqual ( UserApiResponseStatus . success ) ;
61+ } ) ;
62+ } ) ;
63+
64+ describe ( 'updateUserForDatabase' , ( ) => {
65+ it ( 'should return 200 and message' , async ( ) => {
66+ const { uId : uIdAdded } = await await usersClient . addUserToDatabase ( config . database , testEmail ) . then ( response => response . json ( ) ) ;
67+ const response = await usersClient . updateUserForDatabase ( config . database , testEmail , false ) ;
68+ const body = await response . json ( ) ;
69+ expect ( response . status ) . toEqual ( 200 ) ;
70+ expect ( body . status ) . toEqual ( UserApiResponseStatus . success ) ;
71+ expect ( uIdAdded ) . toEqual ( body . uId ) ;
72+ } ) ;
73+ } ) ;
74+
75+ describe ( 'addUserToCompany' , ( ) => {
76+ it ( 'should return 200 and message' , async ( ) => {
77+ const response = await usersClient . addUserToCompany ( companyId , testEmail ) ;
78+ const body = await response . json ( ) ;
79+ expect ( response . status ) . toEqual ( 200 ) ;
80+ expect ( body . status ) . not . toEqual ( UserApiResponseStatus . fail ) ;
81+ } ) ;
82+ } ) ;
83+
84+ describe ( 'removeUserFromCompany' , ( ) => {
85+ it ( 'should return 200 and message' , async ( ) => {
86+ const { uId : uIdAdded } = await await usersClient . addUserToCompany ( companyId , testEmail ) . then ( response => response . json ( ) ) ;
87+ const response = await usersClient . removeUserFromCompany ( companyId , uIdAdded ) ;
88+ const body = await response . json ( ) ;
89+ expect ( response . status ) . toEqual ( 200 ) ;
90+ expect ( body . status ) . not . toEqual ( UserApiResponseStatus . fail ) ;
91+ } ) ;
92+ } ) ;
93+ } ) ;
94+
95+ async function getCompanyId ( apiClient : BugSplatApiClient , database : string ) : Promise < number > {
96+ const rows = await apiClient . fetch < Array < { dbName : string , companyId : string } > > ( '/api/databases.php' ) . then ( response => response . json ( ) ) ;
97+ const row = rows . find ( row => row . dbName === database ) ;
98+ if ( ! row ?. companyId ) {
99+ throw new Error ( `Could not find database ${ database } ` ) ;
100+ }
101+ return Number ( row . companyId ) ;
102+ }
0 commit comments