diff --git a/.env.test b/.env.test new file mode 100644 index 0000000..99805fd --- /dev/null +++ b/.env.test @@ -0,0 +1 @@ +MONGODB_CONNECTION_STRING=mongodb://127.0.0.1:27017/mongoose_test diff --git a/package.json b/package.json index d3718fe..68f632a 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ }, "devDependencies": { "@mongoosejs/migrations": "0.2.5", + "dotenv": "16.4.7", "mocha": "9.1.3", "sinon": "12.0.1" }, diff --git a/test/createReleaseFromChangelog.test.js b/test/createReleaseFromChangelog.test.js index 76ac1c3..2698217 100644 --- a/test/createReleaseFromChangelog.test.js +++ b/test/createReleaseFromChangelog.test.js @@ -13,20 +13,10 @@ const changelog = ` `.trim(); describe('createReleaseFromChangelog', function() { - let Task; - let task; - before(() => { - Task = conn.model('Task', TaskSchema); - }); - - beforeEach(async function() { - task = await Task.create({}); - }); - it('creates a draft release', async function() { sinon.stub(githubOAuth, 'getChangelog').callsFake(() => Promise.resolve(changelog)); sinon.stub(githubOAuth, 'createRelease').callsFake(() => Promise.resolve()); - await createReleaseFromChangelog(task)('6.1.1'); + await createReleaseFromChangelog('6.1.1'); const [tagAndName, body] = githubOAuth.createRelease.getCall(0).args; assert.equal(tagAndName, '6.1.1'); @@ -34,4 +24,4 @@ describe('createReleaseFromChangelog', function() { const [{ branch }] = githubOAuth.getChangelog.getCall(0).args; assert.equal(branch, '6.x'); }); -}); \ No newline at end of file +}); diff --git a/test/githubLogin.test.js b/test/githubLogin.test.js deleted file mode 100644 index 20fd4f9..0000000 --- a/test/githubLogin.test.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -const assert = require('assert'); -const connect = require('../src/db'); -const sinon = require('sinon'); -const githubOAuth = require('../src/integrations/githubOAuth'); -const githubLogin = require('../api_githubLogin').rawFunction; - -describe('githubLogin', function() { - it('handles logging in', async function() { - sinon.stub(githubOAuth, 'getAccessToken').callsFake(() => Promise.resolve({ access_token: '1234' })); - sinon.stub(githubOAuth, 'getUserFromToken').callsFake(() => Promise.resolve({ - login: 'vkarpov15', - id: 'testid' - })); - const { token } = await githubLogin(null, { query: { code: 'testcode' } }); - - assert.ok(githubOAuth.getAccessToken.calledOnceWith('testcode')); - assert.ok(githubOAuth.getUserFromToken.calledOnceWith('1234')); - - const conn = await connect(); - const AccessToken = conn.model('AccessToken'); - const fromDb = await AccessToken.findOne({ _id: token }); - assert.equal(fromDb.githubUserId, 'testid'); - assert.equal(fromDb.githubUserName, 'vkarpov15'); - }); -}); \ No newline at end of file diff --git a/test/setup.test.js b/test/setup.test.js index e06d182..83a78ba 100644 --- a/test/setup.test.js +++ b/test/setup.test.js @@ -1,5 +1,7 @@ 'use strict'; +require('dotenv').config({ path: './.env.test' }); + const connect = require('../src/db'); global.conn = null; @@ -10,4 +12,4 @@ before(async function() { after(async function() { await global.conn.close(); -}); \ No newline at end of file +}); diff --git a/test/webhookGithubApp.test.js b/test/webhookGithubApp.test.js deleted file mode 100644 index a0995a0..0000000 --- a/test/webhookGithubApp.test.js +++ /dev/null @@ -1,73 +0,0 @@ -'use strict'; - -const assert = require('assert'); -const connect = require('../src/db'); -const githubApp = require('../src/integrations/githubApp'); -const sinon = require('sinon'); -const webhookGithubApp = require('../webhookGithubApp').rawFunction; - -describe('webhookGithubApp', function() { - let conn; - let Subscriber; - - before(async function() { - conn = await connect(); - - Subscriber = conn.model('Subscriber'); - }); - - beforeEach(() => conn.dropDatabase()); - - it('works', async function() { - await Subscriber.create({ - email: 'val@karpov.io', - githubUsername: 'vkarpov15', - githubUserId: '1234', - githubOrganization: 'mastering-js' - }); - - const body = { - "action": "created", - "installation": { - "id": 20657751, - "account": { - "login": "mastering-js", - "id": 69875997, - "type": "Organization" - }, - "target_type": "Organization", - "permissions": { - "members": "read", - "organization_events": "read" - }, - "events": [ - "organization" - ], - }, - "repositories": [], - "requester": null, - "sender": { - "login": "vkarpov15", - "id": 1620265, - "type": "User", - "site_admin": false - } - }; - - sinon.stub(githubApp, 'getOrganizationMembers').callsFake(() => Promise.resolve([ - { login: 'vkarpov15', id: '1234', avatar_url: 'dog1.jpg' }, - { login: 'IslandRhythms', id: '5678', avatar_url: 'dog2.jpg' } - ])); - - const res = await webhookGithubApp(null, { body }); - assert.ok(res.ok); - - const sub = await Subscriber.findOne({ githubOrganization: 'mastering-js' }); - assert.ok(sub); - assert.equal(sub.installationId, 20657751); - assert.deepEqual(sub.githubOrganizationMembers.toObject(), [ - { login: 'vkarpov15', id: '1234', avatar: 'dog1.jpg' }, - { login: 'IslandRhythms', id: '5678', avatar: 'dog2.jpg' } - ]); - }); -}); \ No newline at end of file diff --git a/test/webhookGithubSponsors.test.js b/test/webhookGithubSponsors.test.js deleted file mode 100644 index ed55aeb..0000000 --- a/test/webhookGithubSponsors.test.js +++ /dev/null @@ -1,107 +0,0 @@ -'use strict'; - -const assert = require('assert'); -const connect = require('../src/db'); -const api_webhookGithubSponsors = require('../api_webhookGithubSponsors').rawFunction; - -describe('webhookGithubSponsors', function() { - it('creates a new subscriber doc when a new organization subscribes', async function() { - const { subscriber } = await api_webhookGithubSponsors(null, { - body: { - "action": "created", - "sponsorship": { - "sponsor": { - "login": "org", - "id": 2, - "type": "Organization" - }, - "privacy_level": "public", - "tier": { - "name": "Mongoose Pro Subscriber", - "is_one_time": false, - "is_custom_amount": false - } - }, - "sender": { - "login": "user", - "id": 3 - } - } - }); - - const conn = await connect(); - const Subscriber = conn.model('Subscriber'); - const fromDb = await Subscriber.findById(subscriber); - assert.equal(fromDb.githubUserId, 3); - assert.equal(fromDb.githubUsername, 'user'); - assert.equal(fromDb.githubOrganization, 'org'); - assert.equal(fromDb.githubOrganizationId, 2); - }); - - it('creates a new subscriber doc when a new user subscribes', async function() { - const { subscriber } = await api_webhookGithubSponsors(null, { - body: { - "action": "created", - "sponsorship": { - "sponsor": { - "login": "user2", - "id": 2, - "type": "User" - }, - "privacy_level": "public", - "tier": { - "name": "Mongoose Pro Subscriber", - "is_one_time": false, - "is_custom_amount": false - } - }, - "sender": { - "login": "user", - "id": 3 - } - } - }); - - const conn = await connect(); - const Subscriber = conn.model('Subscriber'); - const fromDb = await Subscriber.findById(subscriber); - assert.equal(fromDb.githubUserId, 3); - assert.equal(fromDb.githubUsername, 'user'); - assert.ok(!fromDb.githubOrganization); - assert.ok(!fromDb.githubOrganizationId); - }); - - it('handles incorrect tier names if description still correct', async function() { - const { subscriber } = await api_webhookGithubSponsors(null, { - body: { - "action": "created", - "sponsorship": { - "sponsor": { - "login": "user2", - "id": 2, - "type": "User" - }, - "privacy_level": "public", - "tier": { - "name": "$199 a Month", - "description": "**Mongoose Pro Subscriber**\r\n\r\nThanks for your support!", - "is_one_time": false, - "is_custom_amount": false - } - }, - "sender": { - "login": "user2", - "id": 4 - } - } - }); - - const conn = await connect(); - const Subscriber = conn.model('Subscriber'); - const fromDb = await Subscriber.findById(subscriber); - assert.equal(fromDb.githubUserId, 4); - assert.equal(fromDb.githubUsername, 'user2'); - assert.ok(!fromDb.githubOrganization); - assert.ok(!fromDb.githubOrganizationId); - }); -}); \ No newline at end of file