Skip to content

Commit 7ef7c9c

Browse files
Merge pull request #395 from ProgramEquity/mock-endpoints
Mock-endpoints
2 parents 1677d30 + 194faab commit 7ef7c9c

3 files changed

Lines changed: 1125 additions & 11 deletions

File tree

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,98 @@
1+
/* eslint-disable no-unused-vars */
12
require('dotenv').config()
23
const request = require('supertest')
4+
const axios = require('axios')
35
const express = require('express')
46
const apiRouter = require('../../api')
7+
const apiResponse = require('../../apiResponse')
8+
const ciceroResponse = require('../../ciceroResponse')
59

610
// Create a test application
11+
/* Setting up the environment for the test. */
12+
13+
// Create a test application and get the application id and secret key for the test application from the test application page.
714
const app = express()
815
app.use('/api', apiRouter)
16+
//'https://cicero.azavea.com/v3.1/official'
17+
const zipCode = '84054-6013'
918

1019
afterEach(() => {
1120
jest.clearAllMocks()
1221
})
1322

23+
// Mock the axios request to the Cicero API and return the mock data for the test.
1424
afterAll(async () => {
1525
await new Promise((resolve) => setTimeout(() => resolve(), 500)) // avoid jest open handle error
1626
})
1727

18-
describe('/api/representatives/:zipcode', () => {
19-
test('returns 200 status', async () => {
20-
const response = await request(app).get('/api/representatives/92107')
28+
// Mock the axios request to the Cicero API and return the mock data for the test.
29+
jest.mock('axios')
30+
31+
// Mock the axios request to the Cicero API and return the mock data for the test.
32+
describe('GET /api/representatives/:zipCode', () => {
33+
//test that the api returns a 200 status code and a single representative object in the response
34+
it('should return a 200 status code and a single representative object in the response', async () => {
35+
const spy = jest.spyOn(axios, 'get')
36+
spy.mockImplementation(async (url, { params }) => {
37+
return {
38+
data: ciceroResponse
39+
}
40+
})
41+
// Make a request to the API with half of the zipCode
42+
const response = await request(app).get(
43+
`/api/representatives/${zipCode.slice(0, 5)}`
44+
)
2145
expect(response.status).toBe(200)
22-
})
23-
test('returns 200 status', async () => {
24-
const response = await request(app).get('/api/representatives/92107-1234')
46+
expect(response.body).toEqual(apiResponse)
47+
expect(spy).toHaveBeenCalled()
48+
spy.mockRestore()
49+
}, 50000)
50+
})
51+
52+
// Mock the axios request to the Cicero API and return the mock data for the test.
53+
describe('GET /api/representatives/:zipCode', () => {
54+
//test that the api returns a 200 status code and a single representative object in the response
55+
it('should return a 200 status code and a single representative object in the response', async () => {
56+
const spy = jest.spyOn(axios, 'get')
57+
spy.mockImplementation(async (url, { params }) => {
58+
return {
59+
data: ciceroResponse
60+
}
61+
})
62+
// Make a request to the API
63+
const response = await request(app).get(`/api/representatives/${zipCode}`)
2564
expect(response.status).toBe(200)
26-
})
27-
test('returns 400 status', async () => {
28-
const response = await request(app).get('/api/representatives/cat')
29-
expect(response.status).toBe(400)
30-
})
65+
expect(response.body).toEqual(apiResponse)
66+
expect(spy).toHaveBeenCalled()
67+
spy.mockRestore()
68+
}, 50000)
3169
})
70+
71+
// mock implementation with an invalid zip code which return 400 status code
72+
it('should return a 400 status code and an error message in the response', async () => {
73+
const spy = jest.spyOn(axios, 'get')
74+
// Make a request to the API
75+
const response = await request(app).get(`/api/representatives/invalid`)
76+
expect(response.status).toBe(400)
77+
expect(response.body).toEqual({
78+
error: `Invalid zip code format, valid examples are 84054-6013 or 84054. The zipcode used was invalid`
79+
})
80+
spy.mockRestore()
81+
}, 50000)
82+
83+
// //mock test that gives a 500 status code and an error message in the response when the cicero api is down or not responding to the request from the application server
84+
// it('should return a 500 status code and an error message in the response when the cicero api is down or not responding to the request from the application server', async () => {
85+
// const spy = jest.spyOn(axios, 'get')
86+
// spy.mockImplementation(async (url, { params }) => {
87+
// return {
88+
// data: ciceroResponse
89+
// }
90+
// })
91+
// // Make a request to the API
92+
// const response = await request(app).get(`/api/representatives/${zipCode}`)
93+
// expect(response.status).toBe(500)
94+
// expect(response.body).toEqual({
95+
// error: 'Internal server error'
96+
// })
97+
// spy.mockRestore()
98+
// })

server/apiResponse.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
const apiResponse = [
2+
{
3+
id: 343935,
4+
name: 'Lloyd Austin III',
5+
title: 'Secretary of Defense',
6+
address_line1: 'U.S. Department of Defense',
7+
address_line2: '1000 Defense Pentagon',
8+
address_city: 'Washington',
9+
address_state: 'DC',
10+
address_zip: '20301-1000',
11+
address_country: 'US',
12+
email: 'Not Made Public',
13+
contactPage:
14+
'https://www.defense.gov/our-story/meet-the-team/secretary-of-defense/',
15+
photoUrl:
16+
'https://media.defense.gov/2021/Jan/29/2002592436/825/780/0/210123-A-CN110-0001.JPG',
17+
socialMediaPages: [
18+
{
19+
type: 'facebook',
20+
url: 'https://facebook.com/DeptofDefense',
21+
icon: 'fa-brands fa-facebook-f',
22+
color: '#4267B2'
23+
},
24+
{
25+
type: 'instagram',
26+
url: 'https://instagram.com/austin_lloy',
27+
icon: 'fa-brands fa-instagram',
28+
color: '#C13584'
29+
},
30+
{
31+
type: 'twitter',
32+
url: 'https://twitter.com/SecDef',
33+
icon: 'fa-brands fa-twitter',
34+
color: '#1DA1F2'
35+
},
36+
{
37+
type: 'twitter',
38+
url: 'https://twitter.com/DeptofDefense',
39+
icon: 'fa-brands fa-twitter',
40+
color: '#1DA1F2'
41+
}
42+
],
43+
photoCroppingCSS: 'center center'
44+
},
45+
{
46+
id: 6641,
47+
name: 'Xavier Becerra',
48+
title: 'Secretary of Health and Human Services',
49+
address_line1: 'U.S. Department of Health and Human Services',
50+
address_line2: '200 Independence Avenue, SW',
51+
address_city: 'Washington',
52+
address_state: 'DC',
53+
address_zip: '20201',
54+
address_country: 'US',
55+
email: 'Not Made Public',
56+
contactPage: 'https://www.hhs.gov/about/leadership/xavier-becerra.html',
57+
photoUrl:
58+
'https://www.hhs.gov/sites/default/files/styles/bio_280x280/public/secretary-xavier-becerra.jpg?h=a0d58e75&itok=dCzPbiYp',
59+
socialMediaPages: [
60+
{
61+
type: 'twitter',
62+
url: 'https://twitter.com/SecBecerra',
63+
icon: 'fa-brands fa-twitter',
64+
color: '#1DA1F2'
65+
},
66+
{
67+
type: 'twitter',
68+
url: 'https://twitter.com/XavierBecerra',
69+
icon: 'fa-brands fa-twitter',
70+
color: '#1DA1F2'
71+
}
72+
],
73+
photoCroppingCSS: 'center center'
74+
},
75+
{
76+
id: 343167,
77+
name: 'Tony Blinken',
78+
title: 'Secretary of State',
79+
address_line1: 'Harry S. Truman Building',
80+
address_line2: '2201 C Street, NW',
81+
address_city: 'Washington',
82+
address_state: 'DC',
83+
address_zip: '20520',
84+
address_country: 'US',
85+
email: 'Not Made Public',
86+
contactPage: 'https://register.state.gov/contactus/contactusform',
87+
photoUrl:
88+
'https://www.state.gov/wp-content/uploads/2021/01/50925278638_389ad88a3f_k-1125x1406.jpg',
89+
socialMediaPages: [
90+
{
91+
type: 'facebook',
92+
url: 'https://facebook.com/statedept',
93+
icon: 'fa-brands fa-facebook-f',
94+
color: '#4267B2'
95+
},
96+
{
97+
type: 'instagram',
98+
url: 'https://instagram.com/statedept',
99+
icon: 'fa-brands fa-instagram',
100+
color: '#C13584'
101+
},
102+
{
103+
type: 'instagram',
104+
url: 'https://instagram.com/secblinken',
105+
icon: 'fa-brands fa-instagram',
106+
color: '#C13584'
107+
},
108+
{
109+
type: 'twitter',
110+
url: 'https://twitter.com/SecBlinken',
111+
icon: 'fa-brands fa-twitter',
112+
color: '#1DA1F2'
113+
},
114+
{
115+
type: 'youtube',
116+
url: 'https://youtube.com/statevideo',
117+
icon: 'fa-brands fa-youtube',
118+
color: '#FF0000'
119+
}
120+
],
121+
photoCroppingCSS: 'center center'
122+
}
123+
]
124+
125+
module.exports = apiResponse

0 commit comments

Comments
 (0)