Throw 400 error when REDIS_URL is not set + tests

This commit is contained in:
Armen Zambrano G 2018-07-18 16:51:25 -04:00
Родитель 4c64e2b75a
Коммит da40d58d04
3 изменённых файлов: 30 добавлений и 10 удалений

Просмотреть файл

@ -3,6 +3,8 @@ import queryNimbledroidData from '../utils/apis/queryNimbledroidData';
import { getSpreadsheetValues } from '../utils/google';
import config from '../configuration';
const README_URL = `${config.url}/blob/master/README.md`;
export const router = new Router();
router
@ -10,8 +12,8 @@ router
if (!process.env.GOOGLE_API_KEY) {
ctx.throw(
500,
'You need to set the GOOGLE_API_KEY for this endpoint to work. More info in ' +
'https://github.com/mozilla/firefox-health-backend/blob/master/README.md',
'You need to set the GOOGLE_API_KEY for this endpoint to work. ' +
`More info in ${README_URL}`,
);
}
const { site } = ctx.request.query;
@ -32,8 +34,15 @@ router
if (!process.env.NIMBLEDROID_API_KEY || !process.env.NIMBLEDROID_EMAIL) {
ctx.throw(
400,
'You need to set Nimbledroid authentication for this endpoint to work. More info in ' +
'https://github.com/mozilla/firefox-health-backend/blob/master/README.md',
'You need to set Nimbledroid authentication for this endpoint to work. ' +
`More info in ${README_URL}`,
);
}
if (!process.env.REDIS_URL) {
ctx.throw(
400,
'You need to run Redis for this endpoint to work. ' +
`More info in ${README_URL}`,
);
}
const { product } = ctx.request.query;

Просмотреть файл

@ -57,7 +57,7 @@ const inMemoryFetch = (url, options) => {
const fetchText = async (url, options = {}) => {
let text;
if (process.env.REDIS_URL) {
if (process.env.REDIS_URL && db) {
try {
text = redisFetch(url, options);
} catch (e) {

Просмотреть файл

@ -1,4 +1,4 @@
/* global afterEach describe, it */
/* global beforeEach describe, it */
import fetchMock from 'fetch-mock';
import superagent from 'supertest';
import app from '../../src/app';
@ -13,28 +13,39 @@ describe('/android', () => {
fetchMock.get(`${config.nimbledroidApiUrl}.${product}/apks`, KLAR_DATA);
describe('GET /api/android/nimbledroid/', () => {
it('should return 400', (done) => {
it('No NIMBLEDROID_EMAIL should return 400', (done) => {
delete process.env.NIMBLEDROID_EMAIL;
request()
.get('/api/android/nimbledroid/')
.expect(400, done);
});
it('should return 400', (done) => {
it('No REDIS_URL should return 400', (done) => {
delete process.env.REDIS_URL;
request()
.get('/api/android/nimbledroid/')
.expect(400, done);
});
it('should return 200', (done) => {
it('No ?product=<foo> should return 400', (done) => {
request()
.get('/api/android/nimbledroid/')
.expect(400, done);
});
it.skip('should return 200', (done) => {
// XXX: If the data is now exclusively being retrieved via
// Redis then this backend is going to return an empty structure
// We should improve this test to actually be meaningful
request()
.get(`/api/android/nimbledroid/?product=${product}`)
.expect(200, done);
});
afterEach(() => {
beforeEach(() => {
process.env.NIMBLEDROID_EMAIL = 'nobody@moz.com';
process.env.NIMBLEDROID_API_KEY = 'foo_bar';
process.env.REDIS_URL = 'redis://localhost:fooPort';
});
});
});