Skip to content

Commit

Permalink
Add deforestationAlertsType to v2 PATCH endpoint; additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagojsag committed Mar 10, 2022
1 parent 83ad4aa commit 43ec923
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
11 changes: 9 additions & 2 deletions app/src/routes/api/v2/area.router.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ class AreaRouterV2 {
}

if (ctx.request.body.deforestationAlertsType && !Object.values(gladAlertTypes).includes(ctx.request.body.deforestationAlertsType)) {
ctx.response.status = 400;
ctx.response.body = { message: 'Invalid GLAD alert type.' };
ctx.throw(400, 'Invalid GLAD alert type');
return;
}

Expand Down Expand Up @@ -430,6 +429,14 @@ class AreaRouterV2 {
area.env = updateKeys.includes('env') ? body.env : area.env;
area.fireAlerts = updateKeys.includes('fireAlerts') ? body.fireAlerts : area.fireAlerts;
area.deforestationAlerts = updateKeys.includes('deforestationAlerts') ? body.deforestationAlerts : area.deforestationAlerts;
area.deforestationAlertsType = updateKeys.includes('deforestationAlertsType') ? body.deforestationAlertsType : area.deforestationAlertsType;
if (updateKeys.includes('deforestationAlertsType')) {
if (!Object.values(gladAlertTypes).includes(body.deforestationAlertsType)) {
ctx.throw(400, 'Invalid GLAD alert type');
return;
}
area.deforestationAlertsType = body.deforestationAlertsType;
}
area.monthlySummary = updateKeys.includes('monthlySummary') ? body.monthlySummary : area.monthlySummary;
area.subscriptionId = updateKeys.includes('subscriptionId') ? body.subscriptionId : area.subscriptionId;
area.email = updateKeys.includes('email') ? body.email : area.email;
Expand Down
3 changes: 2 additions & 1 deletion app/test/e2e/v2/create-area.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ describe('V2 - Create area', () => {
});

response.status.should.equal(400);
response.body.should.have.property('message', 'Invalid GLAD alert type.');
response.body.should.have.property('errors').and.be.an('array');
response.body.errors[0].should.have.property('detail').and.equal(`Invalid GLAD alert type`);
});

it('Creating an area with a custom GLAD alert type triggers a request to create a subscription with correct data and returns 200 OK with the created area object', async () => {
Expand Down
2 changes: 2 additions & 0 deletions app/test/e2e/v2/get-single-area.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ describe('V2 - Get single area', () => {
userId: USERS.USER.id,
subscriptionId: id,
name: 'Area name',
deforestationAlertsType: 'glad-all',
})).save();

mockSubscriptionFindByIds([id], {
Expand All @@ -104,6 +105,7 @@ describe('V2 - Get single area', () => {
response.body.data.should.have.property('id').and.equal(area.id);
response.body.data.should.have.property('attributes').and.be.an('object');
response.body.data.attributes.should.have.property('name').and.equal('Area name');
response.body.data.attributes.should.have.property('deforestationAlertsType').and.equal('glad-all');
response.body.data.attributes.should.have.property('confirmed').and.equal(false);
});

Expand Down
24 changes: 23 additions & 1 deletion app/test/e2e/v2/update-area.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,40 @@ describe('V2 - Update area', () => {
const response = await requester
.patch(`/api/v2/area/${testArea.id}`)
.set('Authorization', 'Bearer abcd')
.send({ deforestationAlerts: true });
.send({
deforestationAlerts: true,
deforestationAlertsType: 'glad-all',
});

response.status.should.equal(200);
response.body.should.have.property('data').and.be.an('object');
response.body.data.should.have.property('type').and.equal('area');
response.body.data.should.have.property('id').and.equal(testArea.id);
response.body.data.attributes.should.have.property('subscriptionId').and.equal('5e3bf82fad36f4001abe150e');
response.body.data.attributes.should.have.property('deforestationAlertsType').and.equal('glad-all');
response.body.data.attributes.should.have.property('createdAt');
response.body.data.attributes.should.have.property('updatedAt');
new Date(response.body.data.attributes.updatedAt).should.afterTime(new Date(response.body.data.attributes.createdAt));
});


it('Updating an area with an invalid alert type should return a 400 error message', async () => {
mockGetUserFromToken(USERS.USER);

const testArea = await new Area(createArea({ userId: USERS.USER.id })).save();

const response = await requester
.patch(`/api/v2/area/${testArea.id}`)
.set('Authorization', 'Bearer abcd')
.send({
deforestationAlertsType: 'potato',
});

response.status.should.equal(400);
response.body.should.have.property('errors').and.be.an('array');
response.body.errors[0].should.have.property('detail').and.equal(`Invalid GLAD alert type`);
});

it('Updating an area that had a subscription attached but now with different values updates the subscription and should return a 200 HTTP code and the updated area object', async () => {
mockGetUserFromToken(USERS.USER);

Expand Down
2 changes: 1 addition & 1 deletion app/test/e2e/v2/update-by-geostore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ nock.enableNetConnect(process.env.HOST_IP);

const requester = getTestServer();

describe('V2 - Update area', () => {
describe('V2 - Update area by geostore', () => {
before(() => {
if (process.env.NODE_ENV !== 'test') {
throw Error(`Running the test suite with NODE_ENV ${process.env.NODE_ENV} may result in permanent data loss. Please use NODE_ENV=test.`);
Expand Down

0 comments on commit 43ec923

Please sign in to comment.