@@ -13,162 +13,185 @@ const app = proxyquire('../src/app', {
13
13
'./db' : db
14
14
} ) ;
15
15
16
- describe ( 'app' , ( ) => {
17
- beforeEach ( ( ) => {
18
- db . query = ( ) => Promise . resolve ( ) ;
16
+ const handleIfRouteNotice = ( route , arr ) => {
17
+ if ( route === 'v1.1' ) {
18
+ return arr . map ( object => {
19
+ return { ...object , notice : noticeValue }
20
+ } ) ;
21
+ }
22
+ return arr
23
+ }
24
+
25
+ const routes = [ 'v1.1' , 'v2' ]
26
+
27
+ routes . forEach ( route => {
28
+
29
+ describe ( `app with ${ route } ` , ( ) => {
30
+ beforeEach ( ( ) => {
31
+ db . query = ( ) => Promise . resolve ( ) ;
32
+ } ) ;
33
+
34
+ it ( 'should pass params from the url to db.query and render the result' , done => {
35
+ db . query = ( params ) => {
36
+ expect ( params . reportAgency ) . to . equal ( 'fake-agency' ) ;
37
+ expect ( params . reportName ) . to . equal ( 'fake-report' ) ;
38
+ const arr = handleIfRouteNotice ( route , [
39
+ { id : 1 , date : new Date ( '2017-01-01' ) } ,
40
+ { id : 2 , date : new Date ( '2017-01-02' ) }
41
+ ] )
42
+ return Promise . resolve ( arr )
43
+ } ;
44
+
45
+ const dataRequest = request ( app )
46
+ . get ( `/${ route } /agencies/fake-agency/reports/fake-report/data` )
47
+ . expect ( 200 ) ;
48
+
49
+ dataRequest . then ( response => {
50
+ const arr = handleIfRouteNotice ( route , [
51
+ { id : 1 , date : '2017-01-01' } ,
52
+ { id : 2 , date : '2017-01-02' }
53
+ ] )
54
+ expect ( response . body ) . to . deep . equal ( arr ) ;
55
+ done ( ) ;
56
+ } ) . catch ( done ) ;
57
+ } ) ;
58
+
59
+ it ( 'should not pass the agency param if the request does not specify an agency' , done => {
60
+ db . query = ( params ) => {
61
+ expect ( params . reportAgency ) . to . be . undefined ;
62
+ expect ( params . reportName ) . to . equal ( 'fake-report' ) ;
63
+ const arr = handleIfRouteNotice ( route , [
64
+ { id : 1 , date : new Date ( '2017-01-01' ) } ,
65
+ { id : 2 , date : new Date ( '2017-01-02' ) }
66
+ ] )
67
+ return Promise . resolve ( arr ) ;
68
+ } ;
69
+
70
+ const dataRequest = request ( app )
71
+ . get ( `/${ route } /reports/fake-report/data` )
72
+ . expect ( 200 ) ;
73
+
74
+ dataRequest . then ( response => {
75
+ const arr = handleIfRouteNotice ( route , [
76
+ { id : 1 , date : '2017-01-01' } ,
77
+ { id : 2 , date : '2017-01-02' }
78
+ ] )
79
+ expect ( response . body ) . to . deep . equal ( arr ) ;
80
+ done ( ) ;
81
+ } ) . catch ( done ) ;
82
+ } ) ;
83
+
84
+ it ( 'should merge the params in the url with query params' , done => {
85
+ db . query = ( params ) => {
86
+ expect ( params . reportAgency ) . to . equal ( 'fake-agency' ) ;
87
+ expect ( params . reportName ) . to . equal ( 'fake-report' ) ;
88
+ expect ( params . limit ) . to . equal ( '50' ) ;
89
+ const arr = handleIfRouteNotice ( route , [
90
+ { id : 1 , date : new Date ( '2017-01-01' ) } ,
91
+ { id : 2 , date : new Date ( '2017-01-02' ) }
92
+ ] )
93
+ return Promise . resolve ( arr ) ;
94
+ } ;
95
+
96
+ const dataRequest = request ( app )
97
+ . get ( `/${ route } /agencies/fake-agency/reports/fake-report/data?limit=50` )
98
+ . expect ( 200 ) ;
99
+
100
+ dataRequest . then ( response => {
101
+ const arr = handleIfRouteNotice ( route , [
102
+ { id : 1 , date : '2017-01-01' } ,
103
+ { id : 2 , date : '2017-01-02' }
104
+ ] )
105
+ expect ( response . body ) . to . deep . equal ( arr ) ;
106
+ done ( ) ;
107
+ } ) . catch ( done ) ;
108
+ } ) ;
109
+
110
+ it ( 'should respond with a 400 if db.query rejects' , done => {
111
+ db . query = ( ) => Promise . reject ( 'This is a test of the emergency broadcast system.' ) ;
112
+
113
+ const dataRequest = request ( app )
114
+ . get ( `/${ route } /agencies/fake-agency/reports/fake-report/data` )
115
+ . expect ( 400 ) ;
116
+
117
+ dataRequest . then ( response => {
118
+ expect ( response . body ) . to . deep . equal ( {
119
+ message : 'An error occurred. Please check the application logs.' ,
120
+ status : 400
121
+ } ) ;
122
+ done ( ) ;
123
+ } ) . catch ( done ) ;
124
+ } ) ;
125
+
126
+ it ( 'should respond with a 400 if the domain report is not one of the acceptable kinds of reports' , done => {
127
+ db . query = ( params ) => {
128
+ expect ( params . domain ) . to . equal ( 'fakeiscool.gov' ) ;
129
+ expect ( params . reportName ) . to . equal ( 'browser' ) ;
130
+ return Promise . resolve ( [
131
+ { id : 1 , date : new Date ( '2017-01-01' ) , data : { domain : 'fakeiscool.gov' } } ,
132
+ { id : 2 , date : new Date ( '2017-01-02' ) , data : { domain : 'bobtown.gov' } }
133
+ ] ) ;
134
+ } ;
135
+
136
+ const dataRequest = request ( app )
137
+ . get ( `/${ route } /domain/fakeiscool.gov/reports/browser/data` )
138
+ . expect ( 400 ) ;
139
+
140
+ dataRequest . then ( response => {
141
+ expect ( response . body ) . to . deep . equal ( {
142
+ message : 'You are requesting a report that cannot be filtered on domain. Please try one of the following reports: site, domain, download, second-level-domain.' ,
143
+ status : 400
144
+ } ) ;
145
+ done ( ) ;
146
+ } ) . catch ( done ) ;
147
+ } ) ;
148
+
149
+ it ( 'should pass params from the url to db.query and render the result for a domain query for a non-download report' , done => {
150
+ db . query = ( params ) => {
151
+ expect ( params . domain ) . to . equal ( 'fakeiscool.gov' ) ;
152
+ expect ( params . reportName ) . to . equal ( 'site' ) ;
153
+ const arr = handleIfRouteNotice ( route , [ { id : 1 , date : new Date ( '2017-01-01' ) , report_name : 'site' , data : { domain : 'fakeiscool.gov' } }
154
+ ] )
155
+ return Promise . resolve ( arr ) ;
156
+ } ;
157
+
158
+ const dataRequest = request ( app )
159
+ . get ( `/${ route } /domain/fakeiscool.gov/reports/site/data` )
160
+ . expect ( 200 ) ;
161
+
162
+ dataRequest . then ( response => {
163
+ const arr = handleIfRouteNotice ( route , [
164
+ { id : 1 , date : '2017-01-01' , report_name : 'site' , domain : 'fakeiscool.gov' }
165
+ ] )
166
+ expect ( response . body ) . to . deep . equal ( arr ) ;
167
+ done ( ) ;
168
+ } ) . catch ( done ) ;
169
+ } ) ;
170
+
171
+ it ( 'should pass params from the url to db.query and render the result for a domain query for a download report' , done => {
172
+ db . query = ( params ) => {
173
+ expect ( params . domain ) . to . equal ( 'fakeiscool.gov' ) ;
174
+ expect ( params . reportName ) . to . equal ( 'download' ) ;
175
+ const arr = handleIfRouteNotice ( route , [
176
+ { id : 1 , date : new Date ( '2017-01-01' ) , report_name : 'download' , data : { page : 'fakeiscool.gov/w8.pdf' } } ,
177
+ { id : 2 , date : new Date ( '2017-01-02' ) , report_name : 'download' , data : { page : 'fakeiscool.gov/westworldtheshow/w8.pdf' } } ,
178
+ { id : 3 , date : new Date ( '2017-01-03' ) , report_name : 'download' , data : { page : 'notiscool.gov/westworldtheshow/timewarpagain.pdf' } }
179
+ ] )
180
+ return Promise . resolve ( arr ) ;
181
+ } ;
182
+
183
+ const dataRequest = request ( app )
184
+ . get ( `/${ route } /domain/fakeiscool.gov/reports/download/data` )
185
+ . expect ( 200 ) ;
186
+
187
+ dataRequest . then ( response => {
188
+ const arr = handleIfRouteNotice ( route , [
189
+ { id : 1 , date : '2017-01-01' , page : 'fakeiscool.gov/w8.pdf' , report_name : 'download' } ,
190
+ { id : 2 , date : '2017-01-02' , report_name : 'download' , page : 'fakeiscool.gov/westworldtheshow/w8.pdf' }
191
+ ] )
192
+ expect ( response . body ) . to . deep . equal ( arr ) ;
193
+ done ( ) ;
194
+ } ) . catch ( done ) ;
195
+ } ) ;
19
196
} ) ;
20
-
21
- it ( 'should pass params from the url to db.query and render the result' , done => {
22
- db . query = ( params ) => {
23
- expect ( params . reportAgency ) . to . equal ( 'fake-agency' ) ;
24
- expect ( params . reportName ) . to . equal ( 'fake-report' ) ;
25
- return Promise . resolve ( [
26
- { id : 1 , date : new Date ( '2017-01-01' ) , notice : `${ noticeValue } ` } ,
27
- { id : 2 , date : new Date ( '2017-01-02' ) , notice : `${ noticeValue } ` }
28
- ] ) ;
29
- } ;
30
-
31
- const dataRequest = request ( app )
32
- . get ( '/v1.1/agencies/fake-agency/reports/fake-report/data' )
33
- . expect ( 200 ) ;
34
-
35
- dataRequest . then ( response => {
36
- expect ( response . body ) . to . deep . equal ( [
37
- { id : 1 , date : '2017-01-01' , notice : `${ noticeValue } ` } ,
38
- { id : 2 , date : '2017-01-02' , notice : `${ noticeValue } ` }
39
- ] ) ;
40
- done ( ) ;
41
- } ) . catch ( done ) ;
42
- } ) ;
43
-
44
- it ( 'should not pass the agency param if the request does not specify and agency' , done => {
45
- db . query = ( params ) => {
46
- expect ( params . reportAgency ) . to . be . undefined ;
47
- expect ( params . reportName ) . to . equal ( 'fake-report' ) ;
48
- return Promise . resolve ( [
49
- { id : 1 , date : new Date ( '2017-01-01' ) , notice : `${ noticeValue } ` } ,
50
- { id : 2 , date : new Date ( '2017-01-02' ) , notice : `${ noticeValue } ` }
51
- ] ) ;
52
- } ;
53
-
54
- const dataRequest = request ( app )
55
- . get ( '/v1.1/reports/fake-report/data' )
56
- . expect ( 200 ) ;
57
-
58
- dataRequest . then ( response => {
59
- expect ( response . body ) . to . deep . equal ( [
60
- { id : 1 , date : '2017-01-01' , notice : `${ noticeValue } ` } ,
61
- { id : 2 , date : '2017-01-02' , notice : `${ noticeValue } ` }
62
- ] ) ;
63
- done ( ) ;
64
- } ) . catch ( done ) ;
65
- } ) ;
66
-
67
- it ( 'should merge the params in the url with query params' , done => {
68
- db . query = ( params ) => {
69
- expect ( params . reportAgency ) . to . equal ( 'fake-agency' ) ;
70
- expect ( params . reportName ) . to . equal ( 'fake-report' ) ;
71
- expect ( params . limit ) . to . equal ( '50' ) ;
72
- return Promise . resolve ( [
73
- { id : 1 , date : new Date ( '2017-01-01' ) , notice : `${ noticeValue } ` } ,
74
- { id : 2 , date : new Date ( '2017-01-02' ) , notice : `${ noticeValue } ` }
75
- ] ) ;
76
- } ;
77
-
78
- const dataRequest = request ( app )
79
- . get ( '/v1.1/agencies/fake-agency/reports/fake-report/data?limit=50' )
80
- . expect ( 200 ) ;
81
-
82
- dataRequest . then ( response => {
83
- expect ( response . body ) . to . deep . equal ( [
84
- { id : 1 , date : '2017-01-01' , notice : `${ noticeValue } ` } ,
85
- { id : 2 , date : '2017-01-02' , notice : `${ noticeValue } ` }
86
- ] ) ;
87
- done ( ) ;
88
- } ) . catch ( done ) ;
89
- } ) ;
90
-
91
- it ( 'should respond with a 400 if db.query rejects' , done => {
92
- db . query = ( ) => Promise . reject ( 'This is a test of the emergency broadcast system.' ) ;
93
-
94
- const dataRequest = request ( app )
95
- . get ( '/v1.1/agencies/fake-agency/reports/fake-report/data' )
96
- . expect ( 400 ) ;
97
-
98
- dataRequest . then ( response => {
99
- expect ( response . body ) . to . deep . equal ( {
100
- message : 'An error occurred. Please check the application logs.' ,
101
- status : 400
102
- } ) ;
103
- done ( ) ;
104
- } ) . catch ( done ) ;
105
- } ) ;
106
-
107
- it ( 'should respond with a 400 if the domain report is not one of the acceptable kinds of reports' , done => {
108
- db . query = ( params ) => {
109
- expect ( params . domain ) . to . equal ( 'fakeiscool.gov' ) ;
110
- expect ( params . reportName ) . to . equal ( 'browser' ) ;
111
- return Promise . resolve ( [
112
- { id : 1 , date : new Date ( '2017-01-01' ) , data : { domain : 'fakeiscool.gov' } } ,
113
- { id : 2 , date : new Date ( '2017-01-02' ) , data : { domain : 'bobtown.gov' } }
114
- ] ) ;
115
- } ;
116
-
117
- const dataRequest = request ( app )
118
- . get ( '/v1.1/domain/fakeiscool.gov/reports/browser/data' )
119
- . expect ( 400 ) ;
120
-
121
- dataRequest . then ( response => {
122
- expect ( response . body ) . to . deep . equal ( {
123
- message : 'You are requesting a report that cannot be filtered on domain. Please try one of the following reports: site, domain, download, second-level-domain.' ,
124
- status : 400
125
- } ) ;
126
- done ( ) ;
127
- } ) . catch ( done ) ;
128
- } ) ;
129
-
130
- it ( 'should pass params from the url to db.query and render the result for a domain query for a non-download report' , done => {
131
- db . query = ( params ) => {
132
- expect ( params . domain ) . to . equal ( 'fakeiscool.gov' ) ;
133
- expect ( params . reportName ) . to . equal ( 'site' ) ;
134
- return Promise . resolve ( [
135
- { id : 1 , date : new Date ( '2017-01-01' ) , notice : `${ noticeValue } ` , report_name : 'site' , data : { domain : 'fakeiscool.gov' } }
136
- ] ) ;
137
- } ;
138
-
139
- const dataRequest = request ( app )
140
- . get ( '/v1.1/domain/fakeiscool.gov/reports/site/data' )
141
- . expect ( 200 ) ;
142
-
143
- dataRequest . then ( response => {
144
- expect ( response . body ) . to . deep . equal ( [
145
- { id : 1 , date : '2017-01-01' , notice : `${ noticeValue } ` , report_name : 'site' , domain : 'fakeiscool.gov' }
146
- ] ) ;
147
- done ( ) ;
148
- } ) . catch ( done ) ;
149
- } ) ;
150
-
151
- it ( 'should pass params from the url to db.query and render the result for a domain query for a download report' , done => {
152
- db . query = ( params ) => {
153
- expect ( params . domain ) . to . equal ( 'fakeiscool.gov' ) ;
154
- expect ( params . reportName ) . to . equal ( 'download' ) ;
155
- return Promise . resolve ( [
156
- { id : 1 , date : new Date ( '2017-01-01' ) , notice : `${ noticeValue } ` , report_name : 'download' , data : { page : 'fakeiscool.gov/w8.pdf' } } ,
157
- { id : 2 , date : new Date ( '2017-01-02' ) , notice : `${ noticeValue } ` , report_name : 'download' , data : { page : 'fakeiscool.gov/westworldtheshow/w8.pdf' } } ,
158
- { id : 3 , date : new Date ( '2017-01-03' ) , notice : `${ noticeValue } ` , report_name : 'download' , data : { page : 'notiscool.gov/westworldtheshow/timewarpagain.pdf' } }
159
- ] ) ;
160
- } ;
161
-
162
- const dataRequest = request ( app )
163
- . get ( '/v1.1/domain/fakeiscool.gov/reports/download/data' )
164
- . expect ( 200 ) ;
165
-
166
- dataRequest . then ( response => {
167
- expect ( response . body ) . to . deep . equal ( [
168
- { id : 1 , date : '2017-01-01' , notice : `${ noticeValue } ` , page : 'fakeiscool.gov/w8.pdf' , report_name : 'download' } ,
169
- { id : 2 , date : '2017-01-02' , notice : `${ noticeValue } ` , report_name : 'download' , page : 'fakeiscool.gov/westworldtheshow/w8.pdf' }
170
- ] ) ;
171
- done ( ) ;
172
- } ) . catch ( done ) ;
173
- } ) ;
174
- } ) ;
197
+ } )
0 commit comments