-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #701 from ProgramEquity/615-api-create-campaign-pu…
…t-endpoint-Shangguan Create new v1 Campaign PUT Rest Endpoint
- Loading branch information
Showing
5 changed files
with
99 additions
and
15 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
server/__tests__/integration/server/routes/api/v1/campaigns/campaign_put_endpoint.test.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,56 @@ | ||
const request = require('supertest') | ||
const app = require('../../../../../../../app') | ||
const Campaign = require('../../../../../../../db/models/campaign') | ||
|
||
jest.mock('../../../../../../../db/models/campaign') | ||
|
||
describe('PUT /api/v1/campaigns/:id', () => { | ||
it('should return 200 if successfully updated the campaign', async () => { | ||
const campaignData = { | ||
id: 5, | ||
name: "Sogorea 'Te Land Trust", | ||
organization: "Sogorea 'Te Land Trust", | ||
page_url: 'https://sogoreate-landtrust.org/', | ||
cause: 'Civic Rights', | ||
type: 'Grant' | ||
} | ||
// Mock patchAndFetchById to return a resolved promise | ||
Campaign.query.mockReturnValue({ | ||
patchAndFetchById: jest.fn().mockResolvedValue(campaignData) | ||
}) | ||
const response = await request(app).put('/api/v1/campaigns/5').send({ | ||
campaign: campaignData | ||
}) | ||
expect(response.status).toBe(200) | ||
expect(response.body.campaign).toEqual(campaignData) | ||
}) | ||
|
||
it('should return 400 if no campaign is found', async () => { | ||
// Mock patchAndFetchById to return undefined | ||
Campaign.query.mockReturnValue({ | ||
patchAndFetchById: jest.fn().mockResolvedValue(undefined) | ||
}) | ||
const response = await request(app) | ||
.put('/api/v1/campaigns/999999') | ||
.send({ campaign: { type: 'Accelerator' } }) | ||
|
||
expect(response.status).toBe(404) | ||
expect(response.body.msg).toBe('Campaign not found') | ||
}) | ||
|
||
it('should return 500 for invalid update', async () => { | ||
// Mock patchAndFetchById to throw an error | ||
Campaign.query.mockReturnValue({ | ||
patchAndFetchById: jest.fn().mockRejectedValue(new Error('Server error')) | ||
}) | ||
const response = await request(app) | ||
.put('/api/v1/campaigns/5') | ||
.send({ | ||
campaign: { | ||
type: 'random' | ||
} | ||
}) | ||
|
||
expect(response.status).toBe(500) | ||
}) | ||
}) |
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,28 @@ | ||
const express = require('express') | ||
const Campaign = require('../../../../db/models/campaign') | ||
|
||
const router = express.Router() | ||
|
||
// PUT request to update a campaign | ||
router.put('/:id', async (req, res) => { | ||
const campaignId = req.params.id | ||
|
||
if (!campaignId) return res.status(404).end() | ||
|
||
try { | ||
const updatedCampaign = await Campaign.query().patchAndFetchById( | ||
campaignId, | ||
req.body.campaign | ||
) | ||
if (!updatedCampaign) | ||
return res.status(404).json({ msg: 'Campaign not found' }).end() | ||
|
||
return res.status(200).json({ campaign: updatedCampaign }).end() | ||
} catch (error) { | ||
console.error(error.message) | ||
|
||
return res.status(500).json({ msg: error.message }).end() | ||
} | ||
}) | ||
|
||
module.exports = router |
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,9 @@ | ||
const express = require('express') | ||
const campaigns = require('./campaigns') // If the routes file is named index.js, we can omit it when we do the import instead of writing './campaigns/index'. | ||
|
||
const v1Router = express.Router() | ||
|
||
// Create the final piece of the url /campaigns | ||
v1Router.use('/campaigns', campaigns) | ||
|
||
module.exports = v1Router |