diff --git a/.travis.yml b/.travis.yml index 7b73148..9330520 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,6 @@ notifications: email: false branches: except: -- "/^v\\d+\\.\\d+\\.\\d+$/" env: global: - secure: esvvZQjX1uwAUBdDao5V8dzMFah9NEL3uSb9fbiKcQunegTBZEU3RoAuHywYshghskZ4MXKhe+6vyjAJvVrV5pcD2bdEHQCOEySfoDs+doLwKjH9bPZOefQ7WMOZF9xHAJ/OzhFyQECwH2Z9t3pHAvApraGszUyzSSU+OS0FQKqvdvG65E0mMTc1E60l6lbH12hHs8+cSrM6BUuiesUD8wwWFMABSZFTdkQV7QPlRKy7fSban/PelBDpvqnEQj1j8qCwTnQyakGl0J6BhmlaCtEjaRcSWK80D8CmhoPaFIjz7gR9gxFOpPaVq5jjWtjIIN++N4gsnpQNASt0l9WZTJHl2QOvCdOh2KDa0WSWj81Ic7S0+70+Qfawi4M2wMjiH+pS10dQj9KE/FZoHUGLu4HWvviLkLKX5DfCrQm+ehQPGP/HWslXlFpKrbFGOshn6xhUFZBa+jp1kcZjYDpCACVnNa6GJfr5hF4JlD9/00yafaGovtV56BjSHuj5Zn4KJidZD8xIP7Wg+Vlvhvf08UB+HiBEX+lMJaZRAcFZMg3rPNe9UfdHffk+i7LCWKUdFW4lbd8vn3yj2zzlsfKaOihIS+R+kvuWOX1nHndmC94GF1ZWo28DY55Dqud8AdtAwYn+iQNOfkgA2RSbi8gtarTC5Ehx6GbBKkp2tavVXIc= diff --git a/package-lock.json b/package-lock.json index 4c2547f..82e291f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1256,6 +1256,12 @@ "resolved": "https://registry.npmjs.org/superagent-promise/-/superagent-promise-1.1.0.tgz", "integrity": "sha1-uvIti73UOamwfdEPjAj1T+JQNTM=" }, + "superagent-response-time": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/superagent-response-time/-/superagent-response-time-1.0.3.tgz", + "integrity": "sha1-EYpqYCP7j98+HfFYx8LGaVY4SPg=", + "dev": true + }, "supports-color": { "version": "8.1.1", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz", diff --git a/package.json b/package.json index 49f9d92..86b8b6d 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,8 @@ "http-status-codes": "^2.1.4", "md5": "^2.3.0", "mocha": "^9.0.3", - "mochawesome": "^6.2.2" + "mochawesome": "^6.2.2", + "superagent-response-time": "^1.0.3" }, "dependencies": { "superagent": "^6.1.0", diff --git a/src/GithubRequest.js b/src/GithubRequest.js index d44f8ed..e8113ff 100644 --- a/src/GithubRequest.js +++ b/src/GithubRequest.js @@ -7,6 +7,44 @@ class GithubRequest { .set('User-Agent', 'agent'); return response; } + + async authPut(url) { + const response = await agent + .put(url) + .auth('token', process.env.ACCESS_TOKEN) + .set('User-Agent', 'agent'); + return response; + } + async authPost(url, query) { + const response = await agent + .post(url) + .auth('token', process.env.ACCESS_TOKEN) + .set('User-Agent', 'agent') + .send(query); + return response; + } + async authPatch(url, query) { + const response = await agent + .patch(url) + .auth('token', process.env.ACCESS_TOKEN) + .set('User-Agent', 'agent') + .send(query); + return response; + } + async authDelete(url) { + const response = await agent + .delete(url) + .auth('token', process.env.ACCESS_TOKEN) + .set('User-Agent', 'agent'); + return response; + } + async authHead(url) { + const response = await agent + .head(url) + .auth('token', process.env.ACCESS_TOKEN) + .set('User-Agent', 'agent'); + return response; + } } module.exports = new GithubRequest(); diff --git a/src/data/DeleteMethod.data.js b/src/data/DeleteMethod.data.js new file mode 100644 index 0000000..b2c389c --- /dev/null +++ b/src/data/DeleteMethod.data.js @@ -0,0 +1,19 @@ +deleteData = { + query: { + description: 'gist to try delete method', + public: true, + files: { + 'promise.js': { + content: `const aplicarDescuento = new Promise(function (resolve, reject) { + const descuento = false; + if (descuento) { + resolve("descuento aplicado"); + } else { + reject("no se puede aplicar"); + }});`, + }, + }, + }, +}; + +module.exports = deleteData; diff --git a/src/data/GetMethod.data.js b/src/data/GetMethod.data.js index 8bbfeea..77fabff 100644 --- a/src/data/GetMethod.data.js +++ b/src/data/GetMethod.data.js @@ -14,8 +14,14 @@ getData = { path: 'README.md', sha: '1eb7c4c6f8746fcb3d8767eca780d4f6c393c484', }, - md5Value: 'c95c49b42787e38e0d02793d605395f1', + md5Value: 'a6519e2b84135654bb3ec9b47f114247', md5RawFile: '3449c9e5e332f1dbb81505cd739fbf3f', }; +postData = { + issueInfo: { + title: 'trying github api', + body: null, + }, +}; module.exports = getData; diff --git a/src/data/postMethod.data.js b/src/data/postMethod.data.js new file mode 100644 index 0000000..0040270 --- /dev/null +++ b/src/data/postMethod.data.js @@ -0,0 +1,12 @@ +postData = { + issueInfo: { + title: 'trying github api', + body: null, + }, + issueModifyInfo: { + title: 'trying github api', + body: 'check my body', + }, +}; + +module.exports = postData; diff --git a/test/Github.Put.test.js b/test/Github.Put.test.js new file mode 100644 index 0000000..caee4ee --- /dev/null +++ b/test/Github.Put.test.js @@ -0,0 +1,57 @@ +const statusCode = require('http-status-codes'); +const { expect } = require('chai'); +const githubReq = require('../src/GithubRequest.js'); + +const urlBase = 'https://api.github.com'; +const githubUserName = 'aperdomob'; + +describe('Trying the Github api put method', () => { + let follow; + beforeEach(async () => { + follow = await githubReq.authPut( + `${urlBase}/user/following/${githubUserName}` + ); + }); + it('response after the follow', () => { + expect(follow.status).to.equal(statusCode.NO_CONTENT); + expect(follow.body).to.be.empty; + }); + describe('Check if following a user worked', () => { + let allFollowing; + beforeEach(async () => { + const response = await githubReq.authGet(`${urlBase}/user/following`); + allFollowing = response.body.find( + (user) => user.login === githubUserName + ); + }); + it('shoul be following the user', () => { + expect(allFollowing.login).to.eq(githubUserName); + }); + }); + + describe('Trying to follow the same user again', () => { + let followAgain; + beforeEach(async () => { + followAgain = await githubReq.authPut( + `${urlBase}/user/following/${githubUserName}` + ); + }); + it('response after the follow again', () => { + expect(followAgain.status).to.equal(statusCode.NO_CONTENT); + expect(followAgain.body).to.be.empty; + }); + + describe('Check if following a user again worked', () => { + let allFollowing; + beforeEach(async () => { + const response = await githubReq.authGet(`${urlBase}/user/following`); + allFollowing = response.body.find( + (user) => user.login === githubUserName + ); + }); + it('shoul be following the user', () => { + expect(allFollowing.login).to.eq(githubUserName); + }); + }); + }); +}); diff --git a/test/Github.Redirect.js b/test/Github.Redirect.js new file mode 100644 index 0000000..3d1ce7c --- /dev/null +++ b/test/Github.Redirect.js @@ -0,0 +1,31 @@ +const expect = require('chai').expect; +const statuscode = require('http-status-codes'); +const githubReq = require('../src/GithubRequest.js'); + +const urlBase = 'https://github.com'; +const newUrl = `${urlBase}/aperdomob/new-redirect-test`; +const url = `${urlBase}/aperdomob/redirect-test`; +describe('Trying head method', () => { + let resHead; + beforeEach(async () => { + try { + await githubReq.authHead(url); + } catch (error) { + resHead = error; + } + }); + it('checking the redirect exist', () => { + expect(resHead.status).to.eq(statuscode.MOVED_PERMANENTLY); + expect(resHead.response.header.location).to.equal(newUrl); + }); + + describe('checking the redirect works', () => { + let resRedirect; + beforeEach(async () => { + resRedirect = await githubReq.authGet(url); + }); + it('the url must be the new url', () => { + expect(resRedirect.status).to.eq(statuscode.OK); + }); + }); +}); diff --git a/test/GithubApi.Authentication.test.js b/test/GithubApi.Authentication.test.js index 6a5f1b4..aec5332 100644 --- a/test/GithubApi.Authentication.test.js +++ b/test/GithubApi.Authentication.test.js @@ -9,12 +9,15 @@ const repository = 'workshop-api-testing-js'; describe('Github Api Test', () => { describe('Authentication', () => { it('Via OAuth2 Tokens by Header', async () => { - const response = await agent.get(`${urlBase}/repos/${githubUserName}/${repository}`) + const response = await agent + .get(`${urlBase}/repos/${githubUserName}/${repository}`) .auth('token', process.env.ACCESS_TOKEN) .set('User-Agent', 'agent'); expect(response.status).to.equal(statusCode.OK); - expect(response.body.description).equal('This is a Workshop about Api Testing in JavaScript'); + expect(response.body.description).equal( + 'This is a Workshop about Api Testing in JavaScript' + ); }); it('Via OAuth2 Tokens by parameter', async () => { @@ -31,4 +34,3 @@ describe('Github Api Test', () => { }); }); }); - diff --git a/test/GithubApi.Gist.test.js b/test/GithubApi.Gist.test.js new file mode 100644 index 0000000..be81364 --- /dev/null +++ b/test/GithubApi.Gist.test.js @@ -0,0 +1,59 @@ +const chai = require('chai'); +const expect = require('chai').expect; +const chaiSubset = require('chai-subset'); +const statuscode = require('http-status-codes'); +const githubReq = require('../src/GithubRequest.js'); +const data = require('../src/data/deleteMethod.data'); + +chai.use(chaiSubset); + +const urlBase = 'https://api.github.com'; +describe('Trying delete method', () => { + let gist; + let gistRes; + beforeEach(async () => { + const url = `${urlBase}/gists`; + gistRes = await githubReq.authPost(url, data.query); + gist = gistRes.body; + }); + it('the gist should be created', () => { + expect(gistRes.status).to.eq(statuscode.CREATED); + expect(gist).containSubset(data.query); + }); + + describe('checking if the gist exist', () => { + let gistInfo; + beforeEach(async () => { + res = await githubReq.authGet(gist.url); + gistInfo = res.body; + }); + it('the gist should exist', () => { + expect(gistInfo.id).to.eq(gist.id); + expect(gistInfo).to.exist; + }); + }); + + describe('delete the gist', () => { + let resDelete; + beforeEach(async () => { + resDelete = await githubReq.authDelete(gist.url); + }); + it('the gist should be deleted', () => { + expect(resDelete.status).to.eq(statuscode.NO_CONTENT); + }); + + describe('checking if the gist was delete', () => { + let resCheckDelete; + beforeEach(async () => { + try { + await githubReq.authGet(gist.url); + } catch (error) { + resCheckDelete = error.status; + } + }); + it('the gist should exist', () => { + expect(resCheckDelete).to.eq(statuscode.NOT_FOUND); + }); + }); + }); +}); diff --git a/test/GithubApi.Issu.test.js b/test/GithubApi.Issu.test.js new file mode 100644 index 0000000..50f7cf2 --- /dev/null +++ b/test/GithubApi.Issu.test.js @@ -0,0 +1,56 @@ +const chai = require('chai'); +const expect = require('chai').expect; +const chaiSubset = require('chai-subset'); +const githubReq = require('../src/GithubRequest.js'); +const data = require('../src/data/postMethod.data'); + +chai.use(chaiSubset); + +const urlBase = 'https://api.github.com'; +describe('Trying post/patch methods', () => { + let loginUser; + beforeEach(async () => { + const res = await githubReq.authGet(`${urlBase}/user`); + loginUser = res.body; + }); + it('should have at least one public repo', () => { + expect(loginUser.public_repos).to.be.greaterThan(0); + }); + + describe('getting all repositories', () => { + let repo; + beforeEach(async () => { + const repos = await githubReq.authGet(`${loginUser.repos_url}`); + repo = repos.body[0]; + }); + it('then the repo should exist', () => { + expect(repo).to.exist; + }); + describe('Create an issue', () => { + let issue; + beforeEach(async () => { + const url = `${urlBase}/repos/${loginUser.login}/${repo.name}/issues`; + const res = await githubReq.authPost(url, { + title: 'trying github api', + }); + issue = res.body; + }); + it('the issue should be created', () => { + expect(issue).containSubset(data.issueInfo); + }); + describe('Modify an issue', () => { + let issueModify; + beforeEach(async () => { + const url = `${urlBase}/repos/${loginUser.login}/${repo.name}/issues/${issue.number}`; + const res = await githubReq.authPatch(url, { + body: 'check my body', + }); + issueModify = res.body; + }); + it('the issue should be modified', () => { + expect(issueModify).containSubset(data.issueModifyInfo); + }); + }); + }); + }); +}); diff --git a/test/GithubApi.Users.test.js b/test/GithubApi.Users.test.js new file mode 100644 index 0000000..466a5e4 --- /dev/null +++ b/test/GithubApi.Users.test.js @@ -0,0 +1,54 @@ +const agent = require('superagent'); +const responseTime = require('superagent-response-time'); +const { expect } = require('chai'); + +const urlBase = 'https://api.github.com'; +describe('All the users', () => { + let queryTime; + let response; + before(async () => { + response = await agent + .get(`${urlBase}/users`) + .auth('token', process.env.ACCESS_TOKEN) + .set('User-Agent', 'agent') + .use( + responseTime((res, time) => { + queryTime = time; + }) + ); + }); + + it('then the time should be less than 5seg', () => { + expect(queryTime).to.be.lessThanOrEqual(5000); + }); + it('must be 30 users', () => { + expect(response.body.length).to.eq(30); + }); + + describe('ask for 10 users', () => { + let tenUsers; + beforeEach(async () => { + tenUsers = await agent + .get(`${urlBase}/users`) + .auth('token', process.env.ACCESS_TOKEN) + .set('User-Agent', 'agent') + .query({ per_page: 10 }); + }); + it('then should be 10 users', () => { + expect(tenUsers.body.length).to.eq(10); + }); + }); + describe('ask for 50 users', () => { + let tenUsers; + beforeEach(async () => { + tenUsers = await agent + .get(`${urlBase}/users`) + .auth('token', process.env.ACCESS_TOKEN) + .set('User-Agent', 'agent') + .query({ per_page: 50 }); + }); + it('then should be 10 users', () => { + expect(tenUsers.body.length).to.eq(50); + }); + }); +});