11const { expect } = require ( "chai" ) ;
22const request = require ( "supertest" ) ;
33const jwt = require ( "jsonwebtoken" ) ;
4+ const sinon = require ( "sinon" ) ;
5+ const UserSession = require ( "../../../lib/user_session" ) ;
46const config = require ( "../../../config" ) ;
57
68describe ( "ExemplarIdentifications" , ( ) => {
79 describe ( "search" , ( ) => {
8- const token = jwt . sign (
10+ const adminToken = jwt . sign (
911 { user_id : 1 } ,
1012 config . jwtSecret || "secret" ,
1113 { algorithm : "HS512" }
1214 ) ;
15+
16+ const nonAdminToken = jwt . sign (
17+ { user_id : 123 } ,
18+ config . jwtSecret || "secret" ,
19+ { algorithm : "HS512" }
20+ ) ;
21+
22+ it ( "does not allow unauthenticated requests" , function ( done ) {
23+ request ( this . app ) . get ( "/v2/exemplar_identifications" )
24+ . set ( "Content-Type" , "application/json" )
25+ . expect ( 401 , done ) ;
26+ } ) ;
27+
28+ it ( "does not allow non-admins that are not in relevant test groups" , function ( done ) {
29+ request ( this . app ) . get ( "/v2/exemplar_identifications" )
30+ . set ( "Authorization" , nonAdminToken )
31+ . set ( "Content-Type" , "application/json" )
32+ . expect ( 401 , done ) ;
33+ } ) ;
34+
35+ it ( "does not allow non-admins that not in all relevant test groups" , function ( done ) {
36+ sinon . stub ( UserSession . prototype , "extend" )
37+ . callsFake ( function ( ) {
38+ this . test_groups = [ "helpful-id-tips" ] ;
39+ } ) ;
40+ request ( this . app ) . get ( "/v2/exemplar_identifications" )
41+ . set ( "Authorization" , nonAdminToken )
42+ . set ( "Content-Type" , "application/json" )
43+ . expect ( 401 , ( ) => {
44+ UserSession . prototype . extend . restore ( ) ;
45+ done ( ) ;
46+ } ) ;
47+ } ) ;
48+
49+ it ( "allows non-admins that are in relevant test groups" , function ( done ) {
50+ sinon . stub ( UserSession . prototype , "extend" )
51+ . callsFake ( function ( ) {
52+ this . test_groups = [ "helpful-id-tips-reviewer" , "helpful-id-tips" ] ;
53+ } ) ;
54+ request ( this . app ) . get ( "/v2/exemplar_identifications" )
55+ . set ( "Authorization" , nonAdminToken )
56+ . set ( "Content-Type" , "application/json" )
57+ . expect ( 200 , ( ) => {
58+ UserSession . prototype . extend . restore ( ) ;
59+ done ( ) ;
60+ } ) ;
61+ } ) ;
62+
1363 it ( "returns JSON" , function ( done ) {
1464 request ( this . app ) . get ( "/v2/exemplar_identifications" )
15- . set ( "Authorization" , token )
65+ . set ( "Authorization" , adminToken )
1666 . set ( "Content-Type" , "application/json" )
1767 . expect ( "Content-Type" , / j s o n / )
1868 . expect ( res => {
@@ -25,7 +75,7 @@ describe( "ExemplarIdentifications", ( ) => {
2575
2676 it ( "can include include category_counts" , function ( done ) {
2777 request ( this . app ) . get ( "/v2/exemplar_identifications?direct_taxon_id=3&include_category_counts=true" )
28- . set ( "Authorization" , token )
78+ . set ( "Authorization" , adminToken )
2979 . set ( "Content-Type" , "application/json" )
3080 . expect ( "Content-Type" , / j s o n / )
3181 . expect ( res => {
@@ -36,7 +86,7 @@ describe( "ExemplarIdentifications", ( ) => {
3686
3787 it ( "can include include category_controlled_terms" , function ( done ) {
3888 request ( this . app ) . get ( "/v2/exemplar_identifications?include_category_controlled_terms=true" )
39- . set ( "Authorization" , token )
89+ . set ( "Authorization" , adminToken )
4090 . set ( "Content-Type" , "application/json" )
4191 . expect ( "Content-Type" , / j s o n / )
4292 . expect ( res => {
@@ -47,7 +97,7 @@ describe( "ExemplarIdentifications", ( ) => {
4797
4898 it ( "can filter by upvoted" , function ( done ) {
4999 request ( this . app ) . get ( "/v2/exemplar_identifications?direct_taxon_id=3&upvoted=true&fields=all" )
50- . set ( "Authorization" , token )
100+ . set ( "Authorization" , adminToken )
51101 . set ( "Content-Type" , "application/json" )
52102 . expect ( "Content-Type" , / j s o n / )
53103 . expect ( res => {
@@ -60,7 +110,7 @@ describe( "ExemplarIdentifications", ( ) => {
60110
61111 it ( "can filter by downvoted" , function ( done ) {
62112 request ( this . app ) . get ( "/v2/exemplar_identifications?direct_taxon_id=3&downvoted=true&fields=all" )
63- . set ( "Authorization" , token )
113+ . set ( "Authorization" , adminToken )
64114 . set ( "Content-Type" , "application/json" )
65115 . expect ( "Content-Type" , / j s o n / )
66116 . expect ( res => {
@@ -73,7 +123,7 @@ describe( "ExemplarIdentifications", ( ) => {
73123
74124 it ( "can filter by nominated" , function ( done ) {
75125 request ( this . app ) . get ( "/v2/exemplar_identifications?direct_taxon_id=7&nominated=true&fields=all" )
76- . set ( "Authorization" , token )
126+ . set ( "Authorization" , adminToken )
77127 . set ( "Content-Type" , "application/json" )
78128 . expect ( "Content-Type" , / j s o n / )
79129 . expect ( res => {
@@ -86,7 +136,7 @@ describe( "ExemplarIdentifications", ( ) => {
86136
87137 it ( "can filter by not nominated" , function ( done ) {
88138 request ( this . app ) . get ( "/v2/exemplar_identifications?direct_taxon_id=7&nominated=false&fields=all" )
89- . set ( "Authorization" , token )
139+ . set ( "Authorization" , adminToken )
90140 . set ( "Content-Type" , "application/json" )
91141 . expect ( "Content-Type" , / j s o n / )
92142 . expect ( res => {
@@ -99,7 +149,7 @@ describe( "ExemplarIdentifications", ( ) => {
99149
100150 it ( "can filter by query" , function ( done ) {
101151 request ( this . app ) . get ( "/v2/exemplar_identifications?direct_taxon_id=7&q=unnominated&fields=all" )
102- . set ( "Authorization" , token )
152+ . set ( "Authorization" , adminToken )
103153 . set ( "Content-Type" , "application/json" )
104154 . expect ( "Content-Type" , / j s o n / )
105155 . expect ( res => {
@@ -112,7 +162,7 @@ describe( "ExemplarIdentifications", ( ) => {
112162
113163 it ( "can filter by term_value_id" , function ( done ) {
114164 request ( this . app ) . get ( "/v2/exemplar_identifications?direct_taxon_id=3&term_value_id=2&fields=all" )
115- . set ( "Authorization" , token )
165+ . set ( "Authorization" , adminToken )
116166 . set ( "Content-Type" , "application/json" )
117167 . expect ( "Content-Type" , / j s o n / )
118168 . expect ( res => {
0 commit comments