@@ -4,6 +4,17 @@ const { expect } = chai;
4
4
const cleanDb = require ( "../../utils/cleanDb" ) ;
5
5
const logsQuery = require ( "../../../models/logs" ) ;
6
6
const cacheData = require ( "../../fixtures/cloudflareCache/data" ) ;
7
+ const logsData = require ( "../../fixtures/logs/archievedUsers" ) ;
8
+ const app = require ( "../../../server" ) ;
9
+ const Sinon = require ( "sinon" ) ;
10
+ const { INTERNAL_SERVER_ERROR } = require ( "../../../constants/errorMessages" ) ;
11
+ const userData = require ( "../../fixtures/user/user" ) ( ) ;
12
+ const addUser = require ( "../../utils/addUser" ) ;
13
+ const cookieName = config . get ( "userToken.cookieName" ) ;
14
+ const authService = require ( "../../../services/authService" ) ;
15
+
16
+ const superUser = userData [ 4 ] ;
17
+ const userToBeMadeMember = userData [ 1 ] ;
7
18
8
19
describe ( "Logs" , function ( ) {
9
20
after ( async function ( ) {
@@ -37,4 +48,77 @@ describe("Logs", function () {
37
48
expect ( data [ 0 ] . timestamp . _nanoseconds ) . to . be . a ( "number" ) ;
38
49
} ) ;
39
50
} ) ;
51
+
52
+ describe ( "GET /logs/archived-details" , function ( ) {
53
+ let addLogsStub ;
54
+ let jwt ;
55
+ beforeEach ( async function ( ) {
56
+ const superUserId = await addUser ( superUser ) ;
57
+ jwt = authService . generateAuthToken ( { userId : superUserId } ) ;
58
+ await cleanDb ( ) ;
59
+ } ) ;
60
+ afterEach ( function ( ) {
61
+ Sinon . restore ( ) ;
62
+ } ) ;
63
+
64
+ it ( "Should return an Internal server error message" , async function ( ) {
65
+ addLogsStub = Sinon . stub ( logsQuery , "fetchLogs" ) ;
66
+ addLogsStub . throws ( new Error ( INTERNAL_SERVER_ERROR ) ) ;
67
+
68
+ addUser ( userToBeMadeMember ) . then ( ( ) => {
69
+ const res = chai . request ( app ) . get ( "/logs/archived-details" ) . set ( "cookie" , `${ cookieName } =${ jwt } ` ) . send ( ) ;
70
+
71
+ expect ( res . body . message ) . to . equal ( INTERNAL_SERVER_ERROR ) ;
72
+ } ) ;
73
+ } ) ;
74
+ it ( "Should return empty array if no logs found" , async function ( ) {
75
+ const { type } = logsData . archivedUserDetailsModal [ 0 ] ;
76
+ const query = { } ;
77
+
78
+ const data = await logsQuery . fetchLogs ( query , type ) ;
79
+
80
+ expect ( data ) . to . be . an ( "array" ) . with . lengthOf ( 0 ) ;
81
+ } ) ;
82
+ it ( "Should fetch all archived logs" , async function ( ) {
83
+ const { type, meta, body } = logsData . archivedUserDetailsModal [ 0 ] ;
84
+ const query = { } ;
85
+
86
+ await logsQuery . addLog ( type , meta , body ) ;
87
+ const data = await logsQuery . fetchLogs ( query , type ) ;
88
+
89
+ expect ( data ) . to . be . an ( "array" ) . with . lengthOf . greaterThan ( 0 ) ;
90
+ expect ( data [ 0 ] ) . to . have . property ( "timestamp" ) . that . is . an ( "object" ) ;
91
+ expect ( data [ 0 ] . timestamp ) . to . have . property ( "_seconds" ) . that . is . a ( "number" ) ;
92
+ expect ( data [ 0 ] . timestamp ) . to . have . property ( "_nanoseconds" ) . that . is . a ( "number" ) ;
93
+ expect ( data [ 0 ] . body . archived_user ) . to . have . property ( "username" ) . that . is . a ( "string" ) ;
94
+ expect ( data [ 0 ] . body ) . to . have . property ( "reason" ) . that . is . a ( "string" ) ;
95
+ } ) ;
96
+ it ( "Should fetch all archived logs for given user_id" , async function ( ) {
97
+ const { type, meta, body } = logsData . archivedUserDetailsModal [ 0 ] ;
98
+ const query = {
99
+ userId : body . archived_user . user_id ,
100
+ } ;
101
+ await logsQuery . addLog ( type , meta , body ) ;
102
+ const data = await logsQuery . fetchLogs ( query , type ) ;
103
+
104
+ expect ( data ) . to . be . an ( "array" ) . with . lengthOf . greaterThan ( 0 ) ;
105
+ expect ( data [ 0 ] ) . to . have . property ( "timestamp" ) . that . is . an ( "object" ) ;
106
+ expect ( data [ 0 ] . timestamp ) . to . have . property ( "_seconds" ) . that . is . a ( "number" ) ;
107
+ expect ( data [ 0 ] . timestamp ) . to . have . property ( "_nanoseconds" ) . that . is . a ( "number" ) ;
108
+ expect ( data [ 0 ] . body ) . to . have . property ( "reason" ) . that . is . a ( "string" ) ;
109
+ } ) ;
110
+ it ( "Should throw response status 404, if username is incorrect in the query" , async function ( ) {
111
+ const { type, meta, body } = logsData . archivedUserDetailsModal [ 0 ] ;
112
+ const query = {
113
+ userId : "1234_test" , // incorrect username
114
+ } ;
115
+ await logsQuery . addLog ( type , meta , body ) ;
116
+ const data = await logsQuery . fetchLogs ( query , type ) ;
117
+ const response = await chai . request ( app ) . get ( `/logs/${ type } /${ query } ` ) ;
118
+
119
+ expect ( data ) . to . be . an ( "array" ) . with . lengthOf ( 0 ) ;
120
+ expect ( response ) . to . have . status ( 404 ) ;
121
+ expect ( response . body . message ) . to . be . equal ( "Not Found" ) ;
122
+ } ) ;
123
+ } ) ;
40
124
} ) ;
0 commit comments