-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(general): body campaigns added + testing
- Loading branch information
1 parent
ee8685f
commit 3a4ed73
Showing
12 changed files
with
527 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const { Campaign } = require('../models'); | ||
const helpers = require('../lib/helpers'); | ||
|
||
exports.listAllCampaigns = async (req, res) => { | ||
const result = await Campaign.findAndCountAll({ | ||
where: { autojoin_body_id: req.currentBody.id }, | ||
...helpers.getPagination(req.query), | ||
order: helpers.getSorting(req.query) | ||
}); | ||
|
||
return res.json({ | ||
success: true, | ||
data: result.rows, | ||
meta: { count: result.count } | ||
}); | ||
}; | ||
|
||
exports.getCampaign = async (req, res) => { | ||
// TODO: check permissions | ||
return res.json({ | ||
success: true, | ||
data: req.currentBodyCampaign | ||
}); | ||
}; | ||
|
||
exports.createCampaign = async (req, res) => { | ||
// TODO: check permissions | ||
// TODO: filter out fields that are changed in the other way | ||
const circle = await Campaign.create({ | ||
...req.body, | ||
body_id: req.currentBody.idd | ||
}); | ||
|
||
return res.json({ | ||
success: true, | ||
data: circle | ||
}); | ||
}; | ||
|
||
exports.updateCampaign = async (req, res) => { | ||
// TODO: check permissions | ||
// TODO: filter out fields that are changed in the other way | ||
await req.currentBodyCampaign.update(req.body); | ||
return res.json({ | ||
success: true, | ||
data: req.currentBodyCampaign | ||
}); | ||
}; | ||
|
||
exports.deleteCampaign = async (req, res) => { | ||
// TODO: check permissions | ||
await req.currentBodyCampaign.destroy(); | ||
return res.json({ | ||
success: true, | ||
message: 'Campaign is deleted.' | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
migrations/20200226170830-add-autojoin-body-id-to-campaigns.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.addColumn( | ||
'campaigns', | ||
'autojoin_body_id', | ||
{ | ||
type: Sequelize.INTEGER, | ||
allowNull: true, | ||
references: { | ||
model: 'bodies', | ||
key: 'id' | ||
}, | ||
onDelete: 'SET NULL' | ||
}, | ||
), | ||
down: (queryInterface) => queryInterface.removeColumn('campaigns', 'autojoin_body_id') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
const { startServer, stopServer } = require('../../lib/server.js'); | ||
const { request } = require('../scripts/helpers'); | ||
const generator = require('../scripts/generator'); | ||
|
||
describe('Campaigns creating', () => { | ||
beforeAll(async () => { | ||
await startServer(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stopServer(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await generator.clearAll(); | ||
}); | ||
|
||
test('should fail if there are validation errors', async () => { | ||
const user = await generator.createUser({ username: 'test', mail_confirmed_at: new Date() }); | ||
const token = await generator.createAccessToken({}, user); | ||
const body = await generator.createBody(); | ||
|
||
const campaign = generator.generateCampaign({ name: '' }); | ||
|
||
const res = await request({ | ||
uri: '/bodies/' + body.id + '/campaigns', | ||
method: 'POST', | ||
headers: { 'X-Auth-Token': token.value }, | ||
body: campaign | ||
}); | ||
|
||
expect(res.statusCode).toEqual(422); | ||
expect(res.body.success).toEqual(false); | ||
expect(res.body).not.toHaveProperty('data'); | ||
expect(res.body).toHaveProperty('errors'); | ||
expect(res.body.errors).toHaveProperty('name'); | ||
}); | ||
|
||
test('should succeed if everything is okay', async () => { | ||
const user = await generator.createUser({ username: 'test', mail_confirmed_at: new Date() }); | ||
const token = await generator.createAccessToken({}, user); | ||
const body = await generator.createBody(); | ||
|
||
const campaign = generator.generateCampaign(); | ||
|
||
const res = await request({ | ||
uri: '/bodies/' + body.id + '/campaigns', | ||
method: 'POST', | ||
headers: { 'X-Auth-Token': token.value }, | ||
body: campaign | ||
}); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
expect(res.body.success).toEqual(true); | ||
expect(res.body).not.toHaveProperty('errors'); | ||
expect(res.body).toHaveProperty('data'); | ||
expect(res.body.data.name).toEqual(campaign.name); | ||
}); | ||
|
||
test('should override body id', async () => { | ||
const user = await generator.createUser({ username: 'test', mail_confirmed_at: new Date() }); | ||
const token = await generator.createAccessToken({}, user); | ||
const body = await generator.createBody(); | ||
|
||
const campaign = generator.generateCampaign({ body_id: 1337 }); | ||
|
||
const res = await request({ | ||
uri: '/bodies/' + body.id + '/campaigns', | ||
method: 'POST', | ||
headers: { 'X-Auth-Token': token.value }, | ||
body: campaign | ||
}); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
expect(res.body.success).toEqual(true); | ||
expect(res.body).not.toHaveProperty('errors'); | ||
expect(res.body).toHaveProperty('data'); | ||
expect(res.body.data.body_id).not.toEqual(1337); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
const { startServer, stopServer } = require('../../lib/server.js'); | ||
const { request } = require('../scripts/helpers'); | ||
const generator = require('../scripts/generator'); | ||
const { Campaign } = require('../../models'); | ||
|
||
describe('Body campaigns deleting', () => { | ||
beforeAll(async () => { | ||
await startServer(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stopServer(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await generator.clearAll(); | ||
}); | ||
|
||
test('should return 404 if the campaign is not found', async () => { | ||
const user = await generator.createUser(); | ||
const token = await generator.createAccessToken({}, user); | ||
const body = await generator.createBody(); | ||
|
||
const res = await request({ | ||
uri: '/bodies/' + body.id + '/campaigns/1337', | ||
method: 'DELETE', | ||
headers: { 'X-Auth-Token': token.value } | ||
}); | ||
|
||
expect(res.statusCode).toEqual(404); | ||
expect(res.body.success).toEqual(false); | ||
expect(res.body).not.toHaveProperty('data'); | ||
expect(res.body).toHaveProperty('message'); | ||
}); | ||
|
||
test('should fail for other body\'s campaign', async () => { | ||
const user = await generator.createUser(); | ||
const token = await generator.createAccessToken({}, user); | ||
const body = await generator.createBody(); | ||
const otherBody = await generator.createBody(); | ||
|
||
const campaign = await generator.createCampaign({ autojoin_body_id: otherBody.id }); | ||
|
||
const res = await request({ | ||
uri: '/bodies/' + body.id + '/campaigns/' + campaign.id, | ||
method: 'DELETE', | ||
headers: { 'X-Auth-Token': token.value } | ||
}); | ||
|
||
expect(res.statusCode).toEqual(404); | ||
expect(res.body.success).toEqual(false); | ||
expect(res.body).not.toHaveProperty('data'); | ||
expect(res.body).toHaveProperty('message'); | ||
}); | ||
|
||
test('should succeed if everything is okay', async () => { | ||
const user = await generator.createUser(); | ||
const token = await generator.createAccessToken({}, user); | ||
const body = await generator.createBody(); | ||
|
||
const campaign = await generator.createCampaign({ autojoin_body_id: body.id }); | ||
|
||
const res = await request({ | ||
uri: '/bodies/' + body.id + '/campaigns/' + campaign.id, | ||
method: 'DELETE', | ||
headers: { 'X-Auth-Token': token.value } | ||
}); | ||
|
||
expect(res.statusCode).toEqual(200); | ||
expect(res.body.success).toEqual(true); | ||
expect(res.body).not.toHaveProperty('errors'); | ||
expect(res.body).toHaveProperty('message'); | ||
|
||
const campaignFromDb = await Campaign.findByPk(campaign.id); | ||
expect(campaignFromDb).toEqual(null); | ||
}); | ||
}); |
Oops, something went wrong.