Unverified Commit b9dfcc49 authored by Dan Jones's avatar Dan Jones
Browse files

fix: linting

parent a8e98ce4
......@@ -3,24 +3,19 @@ module.exports = {
env: {
browser: true,
node: true,
'jest/globals': true,
es6: true,
},
parserOptions: {
parser: '@babel/eslint-parser',
// Fix "No Babel config file detected for [file]" error
// https://github.com/babel/babel/issues/11975#issuecomment-786803214
sourceType: 'module',
requireConfigFile: false,
ecmaVersion: 'latest',
},
extends: [
'eslint:recommended',
'prettier',
],
// required to lint *.vue files
plugins: ['vue', 'jest', 'prettier'],
extends: ['eslint:recommended', 'plugin:prettier/recommended'],
ignorePatterns: ['**/dist/'],
// add your custom rules here
rules: {
'no-console': ['warn', { allow: ['warn', 'error'] }],
'no-debugger': 'warn',
'no-console': ['error', { allow: ['warn', 'error'] }],
'no-unused-vars': ['error', { args: 'none' }],
},
};
###
# Place your Prettier ignore content here
###
# .gitignore content is duplicated here due to https://github.com/prettier/prettier/issues/8506
# Created by .ignore support plugin (hsz.mobi)
### Node template
# Logs
/logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Dependency directories
node_modules/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# parcel-bundler cache (https://parceljs.org/)
.cache
# IDE / Editor
.idea
# macOS
.DS_Store
# Vim swap files
*.swp
# Ignore artifacts:
**/dist/
**/*.esm.js
**/*.ssr.js
**/*.min.js
**/.nyc-output/
**/.nuxt/
**/.turbo/
\ No newline at end of file
{
"trailingComma": "es5",
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"bracketSameLine": false,
"arrowParens": "always",
"endOfLine": "lf"
}
module.exports = {
trailingComma: 'es5',
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: true,
bracketSameLine: false,
arrowParens: 'always',
endOfLine: 'lf',
};
module.exports = {
default: {
formatOptions: {
snippetInterface: "synchronous"
snippetInterface: 'synchronous',
},
paths: [ 'test/features/**/*.feature' ],
require: [ 'test/cucumber/**/*.steps.js' ],
paths: ['test/features/**/*.feature'],
require: ['test/cucumber/**/*.steps.js'],
},
};
......@@ -86,7 +86,7 @@ class Adapter {
* @param {boolean} is_retry
* @returns {object}
*/
poll(is_retry=false) {
poll(is_retry = false) {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
......@@ -109,11 +109,13 @@ class Adapter {
})
.catch((error) => {
let retry = false;
switch(error.response.status) {
case 403: {
switch (error.response.status) {
case 403:
{
this.credentials = null;
retry = true;
} break;
}
break;
case 503: {
retry = true;
}
......@@ -132,7 +134,7 @@ class Adapter {
* @param {boolean} is_retry
* @returns
*/
publish(topic, body, is_retry=false) {
publish(topic, body, is_retry = false) {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
......@@ -152,11 +154,13 @@ class Adapter {
})
.catch((error) => {
let retry = false;
switch(error.response.status) {
case 403: {
switch (error.response.status) {
case 403:
{
this.credentials = null;
retry = true;
} break;
}
break;
case 503: {
retry = true;
}
......@@ -178,7 +182,7 @@ class Adapter {
* @param {boolean} is_retry
* @returns
*/
broadcast(body, is_retry=false) {
broadcast(body, is_retry = false) {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
......@@ -197,11 +201,13 @@ class Adapter {
})
.catch((error) => {
let retry = false;
switch(error.response.status) {
case 403: {
switch (error.response.status) {
case 403:
{
this.credentials = null;
retry = true;
} break;
}
break;
case 503: {
retry = true;
}
......
......@@ -88,7 +88,7 @@ class Adapter {
* @param {boolean} is_retry
* @returns {object}
*/
poll(is_retry=false) {
poll(is_retry = false) {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
......@@ -111,11 +111,13 @@ class Adapter {
})
.catch((error) => {
let retry = false;
switch(error.response.status) {
case 403: {
switch (error.response.status) {
case 403:
{
this.credentials = null;
retry = true;
} break;
}
break;
case 503: {
retry = true;
}
......@@ -134,7 +136,7 @@ class Adapter {
* @param {boolean} is_retry
* @returns
*/
publish(topic, body, is_retry=false) {
publish(topic, body, is_retry = false) {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
......@@ -154,11 +156,13 @@ class Adapter {
})
.catch((error) => {
let retry = false;
switch(error.response.status) {
case 403: {
switch (error.response.status) {
case 403:
{
this.credentials = null;
retry = true;
} break;
}
break;
case 503: {
retry = true;
}
......@@ -180,7 +184,7 @@ class Adapter {
* @param {boolean} is_retry
* @returns
*/
broadcast(body, is_retry=false) {
broadcast(body, is_retry = false) {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
......@@ -199,11 +203,13 @@ class Adapter {
})
.catch((error) => {
let retry = false;
switch(error.response.status) {
case 403: {
switch (error.response.status) {
case 403:
{
this.credentials = null;
retry = true;
} break;
}
break;
case 503: {
retry = true;
}
......
......@@ -62,7 +62,7 @@ class GenericProtocol {
getType(message) {
try {
return message.payload.message_type;
} catch(error) {
} catch (error) {
return null;
}
}
......@@ -80,7 +80,6 @@ class GenericProtocol {
return message;
}
/**
* Invoked on receiving an invalid message from the backbone
*
......@@ -106,7 +105,6 @@ class GenericProtocol {
}
}
/**
* GenericSoarProtocol defines a simple passthru handler for messages
*
......@@ -127,9 +125,8 @@ class GenericSoarProtocol extends GenericProtocol {
* @returns {object}
*/
createValidator(schema) {
if (typeof schema === 'string' && schema.match(/^[\w\.]+$/)) {
this.loadSchema(schema)
.then((schema) => {
if (typeof schema === 'string' && schema.match(/^[\w.]+$/)) {
this.loadSchema(schema).then((schema) => {
this.validator = new Validator(schema);
});
} else {
......@@ -144,10 +141,10 @@ class GenericSoarProtocol extends GenericProtocol {
*/
loadSchema(version) {
this.axios = axios;
let repository = "https://git.noc.ac.uk/communications-backbone-system/backbone-message-format";
let repository =
'https://git.noc.ac.uk/communications-backbone-system/backbone-message-format';
let url = `${repository}/-/raw/${version}/project/soar/swagger.json`;
return this.axios.get(url)
.then((response) => {
return this.axios.get(url).then((response) => {
this.schema = response.data;
return response.data;
});
......
......@@ -64,7 +64,7 @@ class GenericProtocol {
getType(message) {
try {
return message.payload.message_type;
} catch(error) {
} catch (error) {
return null;
}
}
......@@ -82,7 +82,6 @@ class GenericProtocol {
return message;
}
/**
* Invoked on receiving an invalid message from the backbone
*
......@@ -108,7 +107,6 @@ class GenericProtocol {
}
}
/**
* GenericSoarProtocol defines a simple passthru handler for messages
*
......@@ -129,9 +127,8 @@ class GenericSoarProtocol extends GenericProtocol {
* @returns {object}
*/
createValidator(schema) {
if (typeof schema === 'string' && schema.match(/^[\w\.]+$/)) {
this.loadSchema(schema)
.then((schema) => {
if (typeof schema === 'string' && schema.match(/^[\w.]+$/)) {
this.loadSchema(schema).then((schema) => {
this.validator = new Validator(schema);
});
} else {
......@@ -146,10 +143,10 @@ class GenericSoarProtocol extends GenericProtocol {
*/
loadSchema(version) {
this.axios = axios;
let repository = "https://git.noc.ac.uk/communications-backbone-system/backbone-message-format";
let repository =
'https://git.noc.ac.uk/communications-backbone-system/backbone-message-format';
let url = `${repository}/-/raw/${version}/project/soar/swagger.json`;
return this.axios.get(url)
.then((response) => {
return this.axios.get(url).then((response) => {
this.schema = response.data;
return response.data;
});
......
......@@ -8,7 +8,6 @@ module.exports = {
'^.+\\.js$': 'babel-jest',
},
collectCoverage: true,
collectCoverageFrom: [
],
collectCoverageFrom: [],
testEnvironment: 'jsdom',
};
......@@ -21,7 +21,7 @@
"copytest:features": "npx recursive-copy -w node_modules/backbone-adapter-testsuite/features test/features",
"copytest:fixtures": "npx recursive-copy -w node_modules/backbone-adapter-testsuite/fixtures test/fixtures",
"copytests": "npm run copytest:features && npm run copytest:fixtures",
"lint:js": "eslint --ext \".js\" --ignore-path .gitignore .",
"lint:js": "eslint -c .eslintrc.js --ext .js --ignore-path .gitignore .",
"lint:prettier": "prettier --check .",
"lint": "yarn lint:js && yarn lint:prettier",
"lintfix": "prettier --write --list-different . && yarn lint:js --fix",
......@@ -51,14 +51,14 @@
"babel-jest": "^27.4.4",
"backbone-adapter-testsuite": "git+https://git.noc.ac.uk/communications-backbone-system/backbone-adapter-testsuite.git#dev",
"cross-env": "^7.0.3",
"eslint": "^8.4.1",
"eslint": "^8.36.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.2.1",
"husky": "^7.0.4",
"jest": "^27.4.4",
"lint-staged": "^12.1.2",
"openapi-schema-validator": "^12.1.0",
"prettier": "^2.5.1",
"prettier": "^2.8.4",
"recursive-copy-cli": "^1.0.20",
"rollup": "^3.9.1"
}
......
......@@ -86,7 +86,7 @@ export class Adapter {
* @param {boolean} is_retry
* @returns {object}
*/
poll(is_retry=false) {
poll(is_retry = false) {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
......@@ -109,11 +109,13 @@ export class Adapter {
})
.catch((error) => {
let retry = false;
switch(error.response.status) {
case 403: {
switch (error.response.status) {
case 403:
{
this.credentials = null;
retry = true;
} break;
}
break;
case 503: {
retry = true;
}
......@@ -132,7 +134,7 @@ export class Adapter {
* @param {boolean} is_retry
* @returns
*/
publish(topic, body, is_retry=false) {
publish(topic, body, is_retry = false) {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
......@@ -152,11 +154,13 @@ export class Adapter {
})
.catch((error) => {
let retry = false;
switch(error.response.status) {
case 403: {
switch (error.response.status) {
case 403:
{
this.credentials = null;
retry = true;
} break;
}
break;
case 503: {
retry = true;
}
......@@ -178,7 +182,7 @@ export class Adapter {
* @param {boolean} is_retry
* @returns
*/
broadcast(body, is_retry=false) {
broadcast(body, is_retry = false) {
let adapterConfig = this.config;
return this.getAuthorizationHeader()
.then((headers) => {
......@@ -197,11 +201,13 @@ export class Adapter {
})
.catch((error) => {
let retry = false;
switch(error.response.status) {
case 403: {
switch (error.response.status) {
case 403:
{
this.credentials = null;
retry = true;
} break;
}
break;
case 503: {
retry = true;
}
......
......@@ -4,7 +4,6 @@ import { GenericProtocol } from '~/modules/comms-adapter/protocol';
*
*/
export class ExampleClientProtocol extends GenericProtocol {
constructor(schema, services) {
super(schema, services);
// bootstrap any injected services
......
......@@ -62,7 +62,7 @@ export class GenericProtocol {
getType(message) {
try {
return message.payload.message_type;
} catch(error) {
} catch (error) {
return null;
}
}
......@@ -80,7 +80,6 @@ export class GenericProtocol {
return message;
}
/**
* Invoked on receiving an invalid message from the backbone
*
......@@ -106,7 +105,6 @@ export class GenericProtocol {
}
}
/**
* GenericSoarProtocol defines a simple passthru handler for messages
*
......@@ -127,9 +125,8 @@ export class GenericSoarProtocol extends GenericProtocol {
* @returns {object}
*/
createValidator(schema) {
if (typeof schema === 'string' && schema.match(/^[\w\.]+$/)) {
this.loadSchema(schema)
.then((schema) => {
if (typeof schema === 'string' && schema.match(/^[\w.]+$/)) {
this.loadSchema(schema).then((schema) => {
this.validator = new Validator(schema);
});
} else {
......@@ -144,11 +141,11 @@ export class GenericSoarProtocol extends GenericProtocol {
*/
loadSchema(version) {
this.axios = axios;
let repository = "https://git.noc.ac.uk/communications-backbone-system/backbone-message-format";
let repository =
'https://git.noc.ac.uk/communications-backbone-system/backbone-message-format';
let url = `${repository}/-/raw/${version}/project/soar/swagger.json`;
return this.axios.get(url)
.then((response) => {
this.schema = response.data
return this.axios.get(url).then((response) => {
this.schema = response.data;
return response.data;
});
}
......
......@@ -3,17 +3,19 @@ const { When, Then } = require('@cucumber/cucumber');
const { fixtures } = require('../../fixtures/server');
When('the auth method is called', async function() {
When('the auth method is called', async function () {
await this.adapter.auth();
});
Then('the adapter credentials are populated', function() {
assert.equal(this.adapter.credentials.token, fixtures.get('response-valid-token').token);
Then('the adapter credentials are populated', function () {
assert.equal(
this.adapter.credentials.token,
fixtures.get('response-valid-token').token
);
});
Then('the adapter auth fails', function() {
this.adapter.auth()
.catch((error) => {
Then('the adapter auth fails', function () {
this.adapter.auth().catch((error) => {
assert.equal(error.response.status, 403);
});
});
const assert = require('assert');
const { Before } = require('@cucumber/cucumber');
const axios = require("axios");
const MockAdapter = require("axios-mock-adapter");
const axios = require('axios');
const MockAdapter = require('axios-mock-adapter');
// This sets the mock adapter on the default instance
const mockAxios = new MockAdapter(axios);
......@@ -23,7 +23,7 @@ const { Adapter } = require('../../../dist/adapter');
* to record what is being tracked.
*/
const tracker = new assert.CallTracker();
const trackedFunction = function(method, params) {
const trackedFunction = function (method, params) {
// do nothing;
};
const recorder = tracker.calls(trackedFunction);
......@@ -51,7 +51,7 @@ class TrackedAdapter extends Adapter {
}
getTrackedCalls(method) {
let calls = this.tracker.getCalls(this.recorder);
let methodCalls = calls.filter(call => call.arguments[0] === method);
let methodCalls = calls.filter((call) => call.arguments[0] === method);
return methodCalls;
}
resetTracker() {
......@@ -65,20 +65,20 @@ class TrackedGenericProtocol extends GenericProtocol {
this.tracker = tracker;
}
encode(type, message) {
this.recorder('encode', {type, message});
return super.encode(type, message)
this.recorder('encode', { type, message });
return super.encode(type, message);
}
decode(type, message) {
this.recorder('decode', {type, message});
return super.decode(type, message)
this.recorder('decode', { type, message });
return super.decode(type, message);
}
validate(message) {
this.recorder('validate', {message});
this.recorder('validate', { message });
return super.validate(message);
}
getTrackedCalls(method) {
let calls = this.tracker.getCalls(this.recorder);
let methodCalls = calls.filter(call => call.arguments[0] === method);
let methodCalls = calls.filter((call) => call.arguments[0] === method);
return methodCalls;
}
resetTracker() {
......@@ -86,35 +86,45 @@ class TrackedGenericProtocol extends GenericProtocol {
}
}
Before(function() {
Before(function () {
this.api = mockValidConfig.api;
this.schema = mockSchema;
this.tracker = tracker;
this.recorder = recorder;
let services = {
recorder,
tracker
tracker,
};
this.classes = {
TrackedAdapter,
TrackedGenericProtocol,
};
this.callCounts = {};
this.protocol = new this.classes.TrackedGenericProtocol(this.schema, services);
this.protocol = new this.classes.TrackedGenericProtocol(
this.schema,
services
);
this.protocol.setupCallTracking(this.recorder, this.tracker);
this.protocol.resetTracker();
this.mockAxios = mockAxios;
this.mockAxios.reset();
this.mockAxios.resetHistory();
this.mockAxios.onGet(
`${mockValidConfig.api}/token`,
{ params: { client_id: mockValidConfig.client_id, secret: mockValidConfig.secret } }
).reply(200, fixtures.get('response-valid-token'));
this.mockAxios.onGet(
`${mockInvalidConfig.api}/token`,
{ params: { client_id: mockInvalidConfig.client_id, secret: mockInvalidConfig.secret } }
).reply(403, fixtures.get('response-denied-token'));
this.mockAxios
.onGet(`${mockValidConfig.api}/token`, {
params: {
client_id: mockValidConfig.client_id,
secret: mockValidConfig.secret,
},
})
.reply(200, fixtures.get('response-valid-token'));
this.mockAxios
.onGet(`${mockInvalidConfig.api}/token`, {
params: {
client_id: mockInvalidConfig.client_id,
secret: mockInvalidConfig.secret,
},
})
.reply(403, fixtures.get('response-denied-token'));
});
......@@ -5,24 +5,25 @@ const { fixtures } = require('../../fixtures/server');
const mockValidConfig = fixtures.get('config-valid');
When('a mock notify API response is configured to return success', function() {
When('a mock notify API response is configured to return success', function () {
const response = {};
this.mockAxios.onPost(
`${mockValidConfig.api}/notify`,
).reply(200, response);
this.mockAxios.onPost(`${mockValidConfig.api}/notify`).reply(200, response);
});
When('a mock notify API response is configured to return a {int} error', function(statusCode) {
When(
'a mock notify API response is configured to return a {int} error',
function (statusCode) {
const statusMessages = {
403: 'Token expired',
503: 'Service unavailable'
503: 'Service unavailable',
};
this.mockAxios.onPost(
`${mockValidConfig.api}/notify`,
).reply(statusCode, { message: statusMessages[statusCode] })
});
this.mockAxios
.onPost(`${mockValidConfig.api}/notify`)
.reply(statusCode, { message: statusMessages[statusCode] });
}
);
When('the broadcast method is called', function() {
When('the broadcast method is called', function () {
const message = fixtures.get('message-vehicle-status');
this.message = message;
const body = JSON.stringify(message);
......@@ -30,7 +31,7 @@ When('the broadcast method is called', function() {
this.callCounts.broadcast = this.adapter.getTrackedCalls('broadcast').length;
});
When('the broadcast method is called with is_retry on', function() {
When('the broadcast method is called with is_retry on', function () {
const message = fixtures.get('message-vehicle-status');
this.message = message;
const body = JSON.stringify(message);
......@@ -38,13 +39,13 @@ When('the broadcast method is called with is_retry on', function() {
this.callCounts.broadcast = this.adapter.getTrackedCalls('broadcast').length;
});
Then('the broadcast method was called with is_retry on', function() {
Then('the broadcast method was called with is_retry on', function () {
let broadcastCalls = this.adapter.getTrackedCalls('broadcast');
let lastCall = broadcastCalls[broadcastCalls.length-1];
let lastCall = broadcastCalls[broadcastCalls.length - 1];
assert.ok(lastCall.arguments[1].is_retry);
});
Then('the broadcast method is not called again', function() {
Then('the broadcast method is not called again', function () {
let newBroadcastCallCount = this.adapter.getTrackedCalls('broadcast').length;
assert.equal(this.callCounts.broadcast, newBroadcastCallCount);
});
......@@ -8,60 +8,71 @@ const mockInvalidConfig = fixtures.get('config-invalid');
const mockSchema = require('../../mock/swagger.json');
// const { Adapter } = require('../../../dist/adapter');
Given('valid config', function() {
this.config = mockValidConfig
Given('valid config', function () {
this.config = mockValidConfig;
});
Given('invalid config', function() {
Given('invalid config', function () {
this.schema = mockSchema;
this.config = mockInvalidConfig;
});
When('the adapter instance is created', function() {
When('the adapter instance is created', function () {
this.adapter = new this.classes.TrackedAdapter(this.protocol, this.config);
this.adapter.setupCallTracking(this.recorder, this.tracker);
});
Then('a successful response is returned with status {int}', function(expectedStatus) {
this.call
.then(response => {
Then(
'a successful response is returned with status {int}',
function (expectedStatus) {
this.call.then((response) => {
assert.equal(response.status, expectedStatus);
});
});
}
);
Then('an error response is returned with status {int}', function(expectedStatus) {
this.call
.catch((error) => {
Then(
'an error response is returned with status {int}',
function (expectedStatus) {
this.call.catch((error) => {
assert.equal(error.response.status, expectedStatus);
});
});
}
);
Then('the credentials are deleted', function() {
Then('the credentials are deleted', function () {
assert.equal(this.adapter.credentials, null);
});
Then('the credentials are not deleted', function() {
Then('the credentials are not deleted', function () {
assert.notEqual(this.adapter.credentials, null);
});
When('the {string} method call counts are checked', function(method) {
When('the {string} method call counts are checked', function (method) {
let newCallCount = this.adapter.getTrackedCalls(method).length;
this.callCounts[method] = newCallCount;
});
Then('the {string} method is not called again', function(method) {
Then('the {string} method is not called again', function (method) {
let newCallCount = this.adapter.getTrackedCalls(method).length;
assert.equal(this.callCounts[method], newCallCount);
});
Then('the total number of calls to {string} was {int}', function(method, expectedCallCount) {
Then(
'the total number of calls to {string} was {int}',
function (method, expectedCallCount) {
let callCount = this.adapter.getTrackedCalls(method).length;
assert.equal(callCount, expectedCallCount);
});
}
);
Then('the total number of {string} requests to {string} was {int}', function(method, endpoint, expectedCallCount) {
Then(
'the total number of {string} requests to {string} was {int}',
function (method, endpoint, expectedCallCount) {
let url = `${this.api}${endpoint}`;
let requestHistory = this.mockAxios.history[method.toLowerCase()].filter((request) => request.url === url);
let requestHistory = this.mockAxios.history[method.toLowerCase()].filter(
(request) => request.url === url
);
assert.equal(requestHistory.length, expectedCallCount);
});
}
);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment