it('should start service', async () => { const OpenWeatherService = proxyquire('../../../services/openweather/index', workingAxiosClear); const openWeatherService = OpenWeatherService(gladys, '35deac79-f295-4adf-8512-f2f48e1ea0f8'); await openWeatherService.start(); });
test('authenticate', async function (t) { let openedUrl = '' const openStub = url => { openedUrl = url return openSuccess() } const authenticate = proxyquire('../lib/authenticate', { open: openStub }) // mocking the browser opening const jwtToken = await authenticate(`http://127.0.0.1:${server.address().port}`) t.plan(2) t.strictEqual(openedUrl.split('/auth/token/')[1].replace('/', ''), cliToken) t.strictEqual(jwtToken, 'jwtToken') })
test('authenticate does not try to open browser in SSH session', async function (t) { const openStub = url => t.fail('should not try to open browser') const origEnv = process.env process.env = { ...origEnv, SSH_CLIENT: '127.0.0.1 1234 22' } // IP PID port t.on('end', () => { process.env = origEnv }) const authenticate = proxyquire('../lib/authenticate', { open: openStub }) const jwtToken = await authenticate(`http://127.0.0.1:${server.address().port}`, { ask: true }) t.ok(jwtToken) })
test('authenticate timeout', async function (t) { const openStub = url => openSuccess() const authenticate = proxyquire('../lib/authenticate', { open: openStub }) // mocking the browser opening simulateTimeout = true t.on('end', () => { simulateTimeout = false }) try { await authenticate(`http://127.0.0.1:${server.address().port}`) t.fail('it should reject') } catch (err) { t.plan(2) t.ok(err) t.ok(err.message.includes('Authentication timed out')) } })
it('should return clear weather formatted', async () => { const OpenWeatherService = proxyquire('../../../services/openweather/index', workingAxiosClear); const openWeatherService = OpenWeatherService(gladys, '35deac79-f295-4adf-8512-f2f48e1ea0f8'); await openWeatherService.start(); const weather = await openWeatherService.weather.get({ latitude: 12, longitude: 10, }); expect(weather).to.deep.equal({ temperature: 17.31, humidity: 48, name: 'Belvedere Tiburon', pressure: 1020, datetime: new Date('2019-03-28T07:50:18.000Z'), units: 'metric', wind_speed: 4.02, wind_direction: 265, weather: 'clear', }); });
test('authenticate for private upload', async function (t) { let openedUrl = '' const openStub = url => { openedUrl = url return openSuccess() } const authenticate = proxyquire('../lib/authenticate', { open: openStub }) // mocking the browser opening const jwtToken = await authenticate(`http://127.0.0.1:${server.address().port}`, { private: true }) const [token, askQuery] = openedUrl.split('/auth/token/')[1].split('?') t.plan(3) t.strictEqual(token.replace('/', ''), cliToken) t.strictEqual(askQuery, 'private=1') t.strictEqual(jwtToken, 'jwtToken') })
it('should stop service', async () => { const OpenWeatherService = proxyquire('../../../services/openweather/index', workingAxiosClear); const openWeatherService = OpenWeatherService(gladys, '35deac79-f295-4adf-8512-f2f48e1ea0f8'); await openWeatherService.stop(); });
test('authenticate failure', async function (t) { const openStub = url => openSuccess() const authenticate = proxyquire( '../lib/authenticate', { open: openStub, split2: () => ({ on: () => [] }) }) try { await authenticate(`http://127.0.0.1:${server.address().port}`) t.fail('it should reject') } catch (err) { t.plan(1) t.ok(err) } })
it('should return error, unable to contact third party provider', async () => { const OpenWeatherService = proxyquire('../../../services/openweather/index', brokenAxios); const openWeatherService = OpenWeatherService(gladys, '35deac79-f295-4adf-8512-f2f48e1ea0f8'); await openWeatherService.start(); const promise = openWeatherService.weather.get({ latitude: 12, longitude: 10, }); return assert.isRejected(promise, 'REQUEST_TO_THIRD_PARTY_FAILED'); });
test('authenticate no auth token', async function (t) { const openStub = url => openSuccess() const authenticate = proxyquire('../lib/authenticate', { open: openStub }) // mocking the browser opening simulateNoToken = true t.on('end', () => { simulateNoToken = false }) try { await authenticate(`http://127.0.0.1:${server.address().port}`) t.fail('it should reject') } catch (err) { t.plan(2) t.ok(err) t.ok(err.message.includes('Authentication failed. No token obtained')) } })
test('authenticate using ask', async function (t) { let openedUrl = '' const openStub = url => { openedUrl = url return openSuccess() } const authenticate = proxyquire('../lib/authenticate', { open: openStub }) // mocking the browser opening const jwtToken = await authenticate(`http://127.0.0.1:${server.address().port}`, { ask: true }) const [token, askQuery] = openedUrl.split('/auth/token/')[1].split('?') t.plan(3) t.strictEqual(token.replace('/', ''), cliToken) t.strictEqual(askQuery, 'ask=1&private=1') t.strictEqual(jwtToken, 'jwtToken') })
it('should return snow weather formatted', async () => { const OpenWeatherService = proxyquire('../../../services/openweather/index', workingAxiosOther); const openWeatherService = OpenWeatherService(gladys, '35deac79-f295-4adf-8512-f2f48e1ea0f8'); await openWeatherService.start(); const weather = await openWeatherService.weather.get({ latitude: 12, longitude: 10, }); expect(weather).to.deep.equal({ temperature: 17.31, humidity: 48, name: 'Belvedere Tiburon', pressure: 1020, datetime: new Date('2019-03-28T07:50:18.000Z'), units: 'metric', wind_speed: 4.02, wind_direction: 265, weather: 'unknown', }); });
it('should return rain weather formatted', async () => { const OpenWeatherService = proxyquire('../../../services/openweather/index', workingAxiosRain); const openWeatherService = OpenWeatherService(gladys, '35deac79-f295-4adf-8512-f2f48e1ea0f8'); await openWeatherService.start(); const weather = await openWeatherService.weather.get({ latitude: 12, longitude: 10, }); expect(weather).to.deep.equal({ temperature: 17.31, humidity: 48, name: 'Belvedere Tiburon', pressure: 1020, datetime: new Date('2019-03-28T07:50:18.000Z'), units: 'metric', wind_speed: 4.02, wind_direction: 265, weather: 'rain', }); });
it('should return snow weather formatted', async () => { const OpenWeatherService = proxyquire('../../../services/openweather/index', workingAxiosSnow); const openWeatherService = OpenWeatherService(gladys, '35deac79-f295-4adf-8512-f2f48e1ea0f8'); await openWeatherService.start(); const weather = await openWeatherService.weather.get({ latitude: 12, longitude: 10, }); expect(weather).to.deep.equal({ temperature: 17.31, humidity: 48, name: 'Belvedere Tiburon', pressure: 1020, datetime: new Date('2019-03-28T07:50:18.000Z'), units: 'metric', wind_speed: 4.02, wind_direction: 265, weather: 'snow', }); });
it('should return snow weather formatted', async () => { const OpenWeatherService = proxyquire('../../../services/openweather/index', workingAxiosDrizzle); const openWeatherService = OpenWeatherService(gladys, '35deac79-f295-4adf-8512-f2f48e1ea0f8'); await openWeatherService.start(); const weather = await openWeatherService.weather.get({ latitude: 12, longitude: 10, }); expect(weather).to.deep.equal({ temperature: 17.31, humidity: 48, name: 'Belvedere Tiburon', pressure: 1020, datetime: new Date('2019-03-28T07:50:18.000Z'), units: 'metric', wind_speed: 4.02, wind_direction: 265, weather: 'drizzle', }); });