Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.

Commit b526536

Browse files
committed
Add api libs for testing
1 parent 1e257fa commit b526536

File tree

11 files changed

+1384
-1
lines changed

11 files changed

+1384
-1
lines changed

.eslintrc.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
extends: [
3+
'@macpaw/eslint-config-webservices/rules/base',
4+
'@macpaw/eslint-config-webservices/rules/filenames',
5+
'@macpaw/eslint-config-webservices/rules/promise',
6+
],
7+
parser: 'espree',
8+
parserOptions: {
9+
ecmaVersion: 8,
10+
sourceType: 'script',
11+
},
12+
plugins: ['async-await'],
13+
rules: {
14+
strict: ['warn', 'global'],
15+
'no-unused-expressions': ['off'],
16+
'newline-after-var': ['off'],
17+
'filenames/match-regex': ['off'],
18+
},
19+
};

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/
2+
node_modules

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
1-
# qa-api-services
1+
# qa-api-services
2+
3+
## Contributing guide
4+
1. Create feature branch and perform changes.
5+
2. Push changes to the origin and create pull-request.
6+
3. After pull-request merging update local master.
7+
4. Bump version and add a tag (for example using `npm version`).
8+
5. Push changes to the origin.
9+
6. Publish new version to NPM.
10+
11+
**TIP**: you can use [np](https://github.com/sindresorhus/np) to perform steps 4-6 automatically
12+
13+
## Versioning rules
14+
Given a version number MAJOR.MINOR.PATCH, increment the:
15+
* **patch** - when you make backwards-compatible bug fixes
16+
* **minor** - when you add functionality in a backwards-compatible manner
17+
* **major** - when you make incompatible API changes

api-services/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports = {
4+
logger: require('./libs/logger'),
5+
request: require('./libs/request'),
6+
requestHelper: require('./libs/request-helper'),
7+
waiters: require('./libs/waiters')
8+
};

api-services/libs/logger.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const chalk = require('chalk');
4+
5+
module.exports = {
6+
info(message) {
7+
console.log(chalk.yellow(message));
8+
},
9+
10+
error(message) {
11+
console.log(chalk.red(message));
12+
},
13+
14+
success(message) {
15+
console.log(chalk.green(message));
16+
},
17+
};

api-services/libs/request-helper.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const request = require('then-request');
4+
5+
module.exports = {
6+
isSuccess(statusCode) {
7+
const regExp = new RegExp('^20[0-9]$');
8+
9+
return regExp.test(statusCode);
10+
},
11+
12+
isServerError(statusCode) {
13+
const regExp = new RegExp('^5[0-1][0-9]$');
14+
15+
return regExp.test(statusCode);
16+
},
17+
18+
async getResponseBody(method, url, options) {
19+
return request(method, url, options);
20+
},
21+
22+
catchError(response) {
23+
const body = response.body.toString();
24+
25+
if (!this.isSuccess(response.statusCode)) {
26+
throw new Error(`Server responded with code ${response.statusCode}: ${body}`);
27+
}
28+
29+
return body ? JSON.parse(body) : {};
30+
},
31+
};

api-services/libs/request.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'use strict';
2+
3+
const requestHelper = require('./request-helper');
4+
5+
module.exports = {
6+
get(url, options) {
7+
const response = browser.call(() => requestHelper.getResponseBody('GET', url, options));
8+
9+
return requestHelper.catchError(response);
10+
},
11+
12+
post(url, options) {
13+
const response = browser.call(() => requestHelper.getResponseBody('POST', url, options));
14+
15+
return requestHelper.catchError(response);
16+
},
17+
18+
patch(url, options) {
19+
const response = browser.call(() => requestHelper.getResponseBody('PATCH', url, options));
20+
21+
return requestHelper.catchError(response);
22+
},
23+
24+
put(url, options) {
25+
const response = browser.call(() => requestHelper.getResponseBody('PUT', url, options));
26+
27+
return requestHelper.catchError(response);
28+
},
29+
30+
del(url, options) {
31+
const response = browser.call(() => requestHelper.getResponseBody('DELETE', url, options));
32+
33+
return requestHelper.catchError(response);
34+
},
35+
};

api-services/libs/waiters.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
module.exports = {
4+
*waitFor(func, strategy, {timeout = 90000, interval = 2500, message, time}) {
5+
const timeStart = Date.now();
6+
7+
const getData = () => new Promise((resolve) => {
8+
setTimeout(() => {
9+
resolve(func());
10+
}, interval);
11+
});
12+
13+
let result = yield getData();
14+
15+
while (true) {
16+
if (strategy(result, time)) {
17+
return result;
18+
} else if (Date.now() - timeStart < timeout) {
19+
result = yield getData();
20+
} else {
21+
throw new Error(message || 'Timeout reached');
22+
}
23+
}
24+
},
25+
};

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
apiServices: require('./api-services/'),
5+
};

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "qa-api-services",
3+
"version": "0.0.0",
4+
"description": "QA API libraries for Macpaw projects",
5+
"main": "index.js",
6+
"author": "Volodymyr Dobrygin",
7+
"contributors": [
8+
"Maksym Shykov",
9+
"Vladyslav Filimonov",
10+
"Andrii Pocherpailo"
11+
],
12+
"repository": {
13+
"type": "git",
14+
"url": "[email protected]:MacPaw/qa-api-services.git"
15+
},
16+
"license": "UNLICENSED",
17+
"scripts": {
18+
"lint": "$(npm bin)/eslint --max-warnings 0 ."
19+
},
20+
"dependencies": {
21+
"chalk": "^2.4.1",
22+
"then-request": "^6.0.2"
23+
},
24+
"devDependencies": {
25+
"@macpaw/eslint-config-webservices": "^1.1.0",
26+
"babel-eslint": "^8.2.3",
27+
"eslint": "^4.19.1",
28+
"eslint-plugin-async-await": "0.0.0",
29+
"eslint-plugin-filenames": "^1.2.0",
30+
"eslint-plugin-promise": "^3.7.0"
31+
}
32+
}

0 commit comments

Comments
 (0)