-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcomputervision.js
More file actions
175 lines (173 loc) · 5.72 KB
/
computervision.js
File metadata and controls
175 lines (173 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const _ = require( "lodash" );
const { expect } = require( "chai" );
const fs = require( "fs" );
const request = require( "supertest" );
const nock = require( "nock" );
const jwt = require( "jsonwebtoken" );
const config = require( "../../../config" );
const fixtures = JSON.parse( fs.readFileSync( "schema/fixtures.js" ) );
describe( "Computervision", ( ) => {
describe( "score_observation", ( ) => {
const obsPhoto = fixtures.postgresql.observation_photos[0];
const fixtureObs = _.find(
fixtures.elasticsearch.observations.observation,
o => o.id === obsPhoto.observation_id
);
const url = `/v2/computervision/score_observation/${fixtureObs.uuid}`;
beforeEach( ( ) => {
const fakeVisionResults = {
results: [{
id: 1,
combined_score: 0.9,
geo_score: 0.9,
geo_threshold: 1.0,
vision_score: 0.9
}, {
id: 2,
combined_score: 0.1,
geo_score: 0.1,
geo_threshold: 0.5,
vision_score: 0.1
}]
};
nock( config.imageProcesing.tensorappURL )
.post( "/" )
.reply( 200, fakeVisionResults );
nock( "https://static.inaturalist.org/photos/1" )
.get( "/medium.jpeg" )
.reply( 200, fs.readFileSync( "test/fixtures/cuthona_abronia-tagged.jpg" ) );
} );
it( "returns JSON", function ( done ) {
const token = jwt.sign(
{ user_id: 333 },
config.jwtSecret || "secret",
{ algorithm: "HS512" }
);
request( this.app ).get( url )
.set( "Authorization", token )
.expect( 200 )
.expect( res => {
expect( res.body.results.length ).to.be.above( 0 );
} )
.expect( "Content-Type", /json/ )
.expect( 200, done );
} );
it( "works with an application token", function ( done ) {
const token = jwt.sign(
{ application: "whatever" },
config.jwtApplicationSecret || "application_secret",
{ algorithm: "HS512" }
);
request( this.app ).get( url )
.set( "Authorization", token )
.expect( 200, done );
} );
it( "should fail without a token", function ( done ) {
request( this.app ).get( url )
.set( "Authorization", "" )
.expect( 401, done );
} );
it( "should fail without a valid token", function ( done ) {
const token = jwt.sign(
{ application: "whatever" },
"not the right secret",
{ algorithm: "HS512" }
);
request( this.app ).get( url )
.set( "Authorization", token )
.expect( 401, done );
} );
it( "should fail without a token that specifies a user or an application", function ( done ) {
const token = jwt.sign(
{ thisIs: "the way" },
config.jwtApplicationSecret || "application_secret",
{ algorithm: "HS512" }
);
request( this.app ).get( url )
.set( "Authorization", token )
.expect( 401, done );
} );
it( "filters for photo id if provided", function ( done ) {
const token = jwt.sign(
{ application: "whatever" },
config.jwtApplicationSecret || "application_secret",
{ algorithm: "HS512" }
);
request( this.app ).get( `${url}?photo_id=1` )
.set( "Authorization", token )
.expect( 200 )
.expect( res => {
expect( res.body.results.length ).to.be.above( 0 );
} )
.expect( 200, done );
} );
it( "should fail when given a photo id not attributed to the observation", function ( done ) {
const token = jwt.sign(
{ application: "whatever" },
config.jwtApplicationSecret || "application_secret",
{ algorithm: "HS512" }
);
request( this.app ).get( `${url}?photo_id=2020101601` )
.set( "Authorization", token )
.expect( 422, done );
} );
} );
describe( "score_image", ( ) => {
const token = jwt.sign(
{ user_id: 333 },
config.jwtSecret || "secret",
{ algorithm: "HS512" }
);
beforeEach( ( ) => {
const fakeVisionResults = {
results: [{
id: 1,
combined_score: 0.9,
geo_score: 0.9,
geo_threshold: 1.0,
vision_score: 0.9
}, {
id: 2,
combined_score: 0.1,
geo_score: 0.1,
geo_threshold: 0.5,
vision_score: 0.1
}]
};
nock( config.imageProcesing.tensorappURL )
.post( "/" )
.reply( 200, fakeVisionResults );
} );
it( "returns JSON", function ( done ) {
request( this.app ).post( "/v2/computervision/score_image" )
.set( "Authorization", token )
.set( "Content-Type", "multipart/form-data" )
.field( "fields", "id,uuid" )
.attach( "image", "test/fixtures/cuthona_abronia-tagged.jpg" )
.expect( 200 )
.expect( res => {
expect( res.body.results.length ).to.be.above( 0 );
} )
.expect( "Content-Type", /json/ )
.expect( 200, done );
} );
it( "allows client to specify fields as JSON", function ( done ) {
request( this.app ).post( "/v2/computervision/score_image" )
.set( "Authorization", token )
.set( "Content-Type", "multipart/form-data" )
.field( "fields", JSON.stringify( {
taxon: ["id", "uuid", "name"]
} ) )
.attach( "image", "test/fixtures/cuthona_abronia-tagged.jpg" )
.expect( 200 )
.expect( res => {
const { taxon } = res.body.results[0];
expect( taxon.id ).not.to.be.undefined;
expect( taxon.uuid ).not.to.be.undefined;
expect( taxon.name ).not.to.be.undefined;
} )
.expect( "Content-Type", /json/ )
.expect( 200, done );
} );
} );
} );