Skip to content

Commit

Permalink
review tests #67
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigura committed Mar 6, 2020
1 parent a88f374 commit 77a8062
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 36 deletions.
45 changes: 30 additions & 15 deletions src/server/firebase/Location.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,6 @@ export async function createLocations(
device,
org,
) {
if (isDeniedCompany(org)) {
throw new AccessDeniedError(
'This is a question from the CEO of Transistor Software.\n' +
'Why are you spamming my demo server1?\n' +
'Please email me at [email protected].',
);
}

if (isDeniedDevice(device.model)) {
throw new AccessDeniedError(
'This is a question from the CEO of Transistor Software.\n' +
'Why are you spamming my demo server2?\n' +
'Please email me at [email protected].',
);
}
const batch = firestore.batch();
await Promise.reduce(
locations,
Expand All @@ -172,6 +157,21 @@ export async function createLocations(
}

export async function create(params) {
if (Array.isArray(params)) {
return Promise.reduce(
params,
async (p, pp) => {
try {
await create(pp);
} catch (e) {
console.error('create', e);
throw e;
}
},
0,
);
}

const {
company_token: token = 'UNKNOWN',
location: list = [],
Expand All @@ -185,6 +185,21 @@ export async function create(params) {
: []
);

if (isDeniedCompany(token)) {
throw new AccessDeniedError(
'This is a question from the CEO of Transistor Software.\n' +
'Why are you spamming my demo server1?\n' +
'Please email me at [email protected].',
);
}

if (isDeniedDevice(device.model)) {
throw new AccessDeniedError(
'This is a question from the CEO of Transistor Software.\n' +
'Why are you spamming my demo server2?\n' +
'Please email me at [email protected].',
);
}
return createLocations(locations, device, token);
}

Expand Down
46 changes: 29 additions & 17 deletions src/server/models/Location.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ export async function getLatestLocation(params, isAdmin) {
}

export async function createLocation(location, deviceInfo, org) {
if (isDeniedDevice(deviceInfo.model)) {
throw new AccessDeniedError(
'This is a question from the CEO of Transistor Software.\n' +
'Why are you spamming my demo server2?\n' +
'Please email me at [email protected].',
);
}

const now = new Date();
const device = await findOrCreate(org, { ...deviceInfo });

Expand Down Expand Up @@ -122,14 +114,6 @@ export async function createLocations(
device,
org,
) {
if (isDeniedCompany(org)) {
throw new AccessDeniedError(
'This is a question from the CEO of Transistor Software.\n' +
'Why are you spamming my demo server1?\n' +
'Please email me at [email protected].',
);
}

return Promise.reduce(
locations,
async (p, location) => {
Expand All @@ -151,6 +135,21 @@ export async function createLocations(
export async function create(
params,
) {
if (Array.isArray(params)) {
return Promise.reduce(
params,
async (p, pp) => {
try {
await create(pp);
} catch (e) {
console.error('create', e);
throw e;
}
},
0,
);
}

const {
company_token: token = 'UNKNOWN',
location: list = [],
Expand All @@ -163,7 +162,20 @@ export async function create(
? [list]
: []
);

if (isDeniedCompany(token)) {
throw new AccessDeniedError(
'This is a question from the CEO of Transistor Software.\n' +
'Why are you spamming my demo server1?\n' +
'Please email me at [email protected].',
);
}
if (isDeniedDevice(device.model)) {
throw new AccessDeniedError(
'This is a question from the CEO of Transistor Software.\n' +
'Why are you spamming my demo server2?\n' +
'Please email me at [email protected].',
);
}
return createLocations(locations, device, token);
}

Expand Down
5 changes: 2 additions & 3 deletions src/server/routes/firebase-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,12 @@ router.get('/locations', checkAuth(verify), async (req, res) => {
* POST /locations
*/
router.post('/locations', checkAuth(verify), async (req, res) => {
const { org } = req.jwt;
const { org, deviceId } = req.jwt;
const { body } = req;
const data = isEncryptedRequest(req)
? decrypt(body.toString())
: body;
const { device: { uuid = 'UNKNOWN' } } = data || { device: {} };
const device = await getDevice({ id: uuid, org });
const device = await getDevice({ id: deviceId, org });
// eslint-disable-next-line no-console
console.info(
'v3',
Expand Down
10 changes: 10 additions & 0 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ describe('jwt api', () => {
expect(res).to.be.json;
});

test('POST /locations []', async () => {
const res = await chai
.request(server)
.post('/api/jwt/locations')
.set('Authorization', `Bearer ${token}`)
.send([{ location }]);
expect(res).have.status(200);
expect(res).to.be.json;
});

test('/locations', async () => {
const res = await chai
.request(server)
Expand Down
16 changes: 15 additions & 1 deletion tests/firebase.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ beforeAll(async () => {
({ accessToken: token } = res.body);
});

describe('jwt api', () => {
describe('firebase api', () => {
test('/register', async () => {
const res = await chai
.request(server)
Expand Down Expand Up @@ -152,6 +152,20 @@ describe('jwt api', () => {
expect(res).to.be.json;
});

test('POST /locations []', async () => {
const res = await chai
.request(server)
.post('/api/firebase/locations')
.set('Authorization', `Bearer ${token}`)
.send([{
location,
device: { model: 'test', uuid: 'uuid' },
company_token: 'org',
}]);
expect(res).have.status(200);
expect(res).to.be.json;
});

test('/locations', async () => {
const res = await chai
.request(server)
Expand Down
14 changes: 14 additions & 0 deletions tests/site-api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ describe('site api', () => {
expect(res).to.be.json;
});

test('POST /locations []', async () => {
const res = await chai
.request(server)
.post('/api/site/locations')
.set('Authorization', `Bearer ${token}`)
.send([{
location,
device: { model: 'test', uuid: 'test' },
company_token: 'test',
}]);
expect(res).have.status(200);
expect(res).to.be.json;
});

test('/locations', async () => {
const res = await chai
.request(server)
Expand Down

0 comments on commit 77a8062

Please sign in to comment.