Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions __tests__/integration/capture/capture.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ const grower_account1 = require('../../mock/grower_account1.json');
const grower_account2 = require('../../mock/grower_account2.json');
const domain_event2 = require('../../mock/domain_event2.json');
const tree1 = require('../../mock/tree1.json');
const { knex, addCapture, addTree } = require('../../utils');
const { knex, apiKey, addCapture, addTree, addApiKey } = require('../../utils');

describe('/captures', () => {
let brokerStub;

before(async () => {
await addApiKey();
brokerStub = sinon.stub(Broker, 'create').resolves({
publish: () => {
return {
Expand Down Expand Up @@ -64,6 +65,18 @@ describe('/captures', () => {
await knex('capture').del();
await knex('grower_account').del();
await knex('tree').del();
await knex('api_key').del();
});

it('unauthorized', async () => {
await request(app)
.post(`/captures`)
.send({
...capture2,
})
.set('Accept', 'application/json')
.set('Authorization', 'jwt_token')
.expect(401);
});

describe('POST', () => {
Expand Down Expand Up @@ -105,6 +118,7 @@ describe('/captures', () => {
...capture2,
...legacyExtraObjects,
})
.set('treetracker-api-key', apiKey)
.set('Accept', 'application/json')
.set('Authorization', 'jwt_token')
.expect(201);
Expand Down Expand Up @@ -138,6 +152,7 @@ describe('/captures', () => {
...capture2,
...legacyExtraObjects,
})
.set('treetracker-api-key', apiKey)
.set('Accept', 'application/json')
.set('Authorization', 'jwt_token')
.expect(200);
Expand Down Expand Up @@ -172,6 +187,7 @@ describe('/captures', () => {
...legacyExtraObjects,
id: capture1.id,
})
.set('treetracker-api-key', apiKey)
.set('Accept', 'application/json')
.set('Authorization', 'jwt_token')
.expect(200);
Expand Down Expand Up @@ -213,6 +229,7 @@ describe('/captures', () => {
...legacyExtraObjects,
id,
})
.set('treetracker-api-key', apiKey)
.set('Accept', 'application/json')
.set('Authorization', 'jwt_token')
.expect(200);
Expand Down Expand Up @@ -242,6 +259,7 @@ describe('/captures', () => {
...legacyExtraObjects,
id,
})
.set('treetracker-api-key', apiKey)
.set('Accept', 'application/json')
.set('Authorization', 'jwt_token')
.expect(200);
Expand Down Expand Up @@ -281,6 +299,7 @@ describe('/captures', () => {
const res = await request(app)
.patch(`/captures/${captureId}`)
.send(updates)
.set('treetracker-api-key', apiKey)
.set('Accept', 'application/json')
.expect(200);

Expand Down Expand Up @@ -318,7 +337,10 @@ describe('/captures', () => {
});

it('should get captures', async () => {
const result = await request(app).get(`/captures`).expect(200);
const result = await request(app)
.get(`/captures`)
.set('treetracker-api-key', apiKey)
.expect(200);
expect(result.body.captures.length).to.eql(2);
// console.log(result.body.captures[1]);
// expect(result.body.captures[1]).to.include({ ...capture1 });
Expand All @@ -327,6 +349,7 @@ describe('/captures', () => {
it('should get only captures with tree associated', async () => {
const result = await request(app)
.get(`/captures?tree_associated=true`)
.set('treetracker-api-key', apiKey)
.expect(200);
expect(result.body.captures.length).to.eql(1);
expect(result.body.query.count).to.eql(1);
Expand All @@ -338,6 +361,7 @@ describe('/captures', () => {
it('should get only captures without tree associated', async () => {
const result = await request(app)
.get(`/captures?tree_associated=false`)
.set('treetracker-api-key', apiKey)
.expect(200);
expect(result.body.captures.length).to.eql(1);
expect(result.body.query.count).to.eql(1);
Expand All @@ -349,9 +373,13 @@ describe('/captures', () => {
.patch(`/captures/${captureId}`)
.send({ status: 'deleted' })
.set('Accept', 'application/json')
.set('treetracker-api-key', apiKey)
.expect(200);

const result = await request(app).get(`/captures`).expect(200);
const result = await request(app)
.get(`/captures`)
.set('treetracker-api-key', apiKey)
.expect(200);
expect(result.body.captures.length).to.eql(1);
});

Expand Down Expand Up @@ -388,13 +416,15 @@ describe('/captures', () => {
it('should attach tag(s) to a capture', async () => {
await request(app)
.post(`/captures/${captureId}/tags`)
.set('treetracker-api-key', apiKey)
.send({ tags: [tag2.id] })
.expect(204);
});

it('should not allow duplicate attachments', async () => {
const result = await request(app)
.post(`/captures/${captureId}/tags`)
.set('treetracker-api-key', apiKey)
.send({ tags: [tag2.id] })
.expect(400);

Expand All @@ -407,6 +437,7 @@ describe('/captures', () => {
it('should get tags attached to a capture', async () => {
const result = await request(app)
.get(`/captures/${captureId}/tags`)
.set('treetracker-api-key', apiKey)
.expect(200);
expect(result.body.length).to.eql(1);
expect(result.body[0]).to.include({
Expand All @@ -429,11 +460,13 @@ describe('/captures', () => {
it('should update a single tag attached to a capture', async () => {
await request(app)
.patch(`/captures/${captureId}/tags/${tag2.id}`)
.set('treetracker-api-key', apiKey)
.send({ status: 'deleted' })
.expect(200);

await request(app)
.get(`/captures/${captureId}/tags/${tag2.id}`)
.set('treetracker-api-key', apiKey)
.expect(404);
});
});
Expand Down
42 changes: 39 additions & 3 deletions __tests__/integration/grower_account/grower_account.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ chai.use(require('chai-things'));
const app = require('../../../server/app');
const grower_account1 = require('../../mock/grower_account1.json');
const grower_account2 = require('../../mock/grower_account2.json');
const { knex } = require('../../utils');
const { knex, addApiKey, apiKey } = require('../../utils');
const s3 = require('../../../server/infra/S3/s3');

describe('/grower_account', () => {
Expand All @@ -26,18 +26,38 @@ describe('/grower_account', () => {
show_in_map: true,
};

before(async () => {
await addApiKey();
});

after(async () => {
await knex('grower_account_org').del();
await knex('grower_account_image').del();
await knex('grower_account').del();
await knex('planter').del();
await knex('api_key').del();
});

it('unauthorized', async () => {
await request(app)
.post(`/grower_accounts`)
.send(grower_account1)
.set('Accept', 'application/json')
.expect(401);

await request(app)
.patch(`/grower_accounts/image/imageId`)
.set('treetracker-api-key', 'apiKey')
.send({ active: false })
.expect(401);
});

describe('POST', () => {
it('should create a grower account', async () => {
const res = await request(app)
.post(`/grower_accounts`)
.send(grower_account1)
.set('treetracker-api-key', apiKey)
.set('Accept', 'application/json')
.expect(201);

Expand All @@ -52,6 +72,7 @@ describe('/grower_account', () => {
const res2 = await request(app)
.post(`/grower_accounts`)
.send(grower_account2)
.set('treetracker-api-key', apiKey)
.set('Accept', 'application/json')
.expect(201);

Expand All @@ -68,6 +89,7 @@ describe('/grower_account', () => {
const res = await request(app)
.post(`/grower_accounts`)
.send(grower_account1)
.set('treetracker-api-key', apiKey)
.set('Accept', 'application/json')
.expect(200);

Expand All @@ -89,6 +111,7 @@ describe('/grower_account', () => {
const res = await request(app)
.patch(`/grower_accounts/${grower_account1.id}`)
.send(growerAccountUpdates)
.set('treetracker-api-key', apiKey)
.set('Accept', 'application/json')
.expect(200);

Expand All @@ -110,7 +133,10 @@ describe('/grower_account', () => {
});

it('should get grower account', async () => {
const result = await request(app).get(`/grower_accounts`).expect(200);
const result = await request(app)
.get(`/grower_accounts`)
.set('treetracker-api-key', apiKey)
.expect(200);
expect(result.body.grower_accounts.length).to.eql(2);
expect(result.body.links).to.have.keys(['prev', 'next']);
expect(result.body.grower_accounts)
Expand All @@ -124,6 +150,7 @@ describe('/grower_account', () => {
it('should get grower account -- by id', async () => {
const result = await request(app)
.get(`/grower_accounts/${grower_account1.id}`)
.set('treetracker-api-key', apiKey)
.expect(200);
expect(result.body).to.include({
...grower_account1,
Expand All @@ -136,11 +163,15 @@ describe('/grower_account', () => {
it('should delete a grower account', async () => {
await request(app)
.patch(`/grower_accounts/${grower_account1.id}`)
.set('treetracker-api-key', apiKey)
.send({ status: 'deleted' })
.set('Accept', 'application/json')
.expect(200);

const result = await request(app).get(`/grower_accounts`).expect(200);
const result = await request(app)
.get(`/grower_accounts`)
.set('treetracker-api-key', apiKey)
.expect(200);
expect(result.body.grower_accounts.length).to.eql(1);
expect(result.body.links).to.have.keys(['prev', 'next']);
});
Expand All @@ -152,6 +183,7 @@ describe('/grower_account', () => {
const result = await request(app)
.put(`/grower_accounts`)
.send({ ...grower_account1, wallet: grower_account2.wallet })
.set('treetracker-api-key', apiKey)
.set('Accept', 'application/json')
.expect(200);

Expand Down Expand Up @@ -184,6 +216,7 @@ describe('/grower_account', () => {
const res = await request(app)
.post(`/grower_accounts/image`)
.set('Accept', 'multipart/form-data')
.set('treetracker-api-key', apiKey)
.field('grower_account_id', growerAccountId)
.attach('image', `${__dirname}/../../mock/test.jpeg`)
.expect(201);
Expand All @@ -197,6 +230,7 @@ describe('/grower_account', () => {

const result = await request(app)
.get(`/grower_accounts/${growerAccountId}`)
.set('treetracker-api-key', apiKey)
.expect(200);
expect(result.body.images.length).eql(1);
expect(result.body.images[0].id).eql(imageId);
Expand All @@ -208,13 +242,15 @@ describe('/grower_account', () => {
it('PATCH /grower_accounts/image/:image_id', async () => {
const res = await request(app)
.patch(`/grower_accounts/image/${imageId}`)
.set('treetracker-api-key', apiKey)
.send({ active: false })
.expect(200);

expect(res.body.active).eql(false);

const result = await request(app)
.get(`/grower_accounts/${growerAccountId}`)
.set('treetracker-api-key', apiKey)
.expect(200);
expect(result.body.images.length).eql(0);

Expand Down
Loading