From d080f2a21757080a1868ab96437000ad691387c5 Mon Sep 17 00:00:00 2001 From: jankapunkt Date: Tue, 2 Jul 2024 16:21:46 +0200 Subject: [PATCH 1/3] tests: improved model tests --- tests/model-tests.js | 52 +++++++++++++++++++++++++++++++++++++++----- tests/oauth-tests.js | 7 +++--- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/tests/model-tests.js b/tests/model-tests.js index 6cd1adc..4026c28 100644 --- a/tests/model-tests.js +++ b/tests/model-tests.js @@ -2,7 +2,7 @@ import { Meteor } from 'meteor/meteor' import { Mongo } from 'meteor/mongo' import { Random } from 'meteor/random' -import { assert } from 'meteor/practicalmeteor:chai' +import { assert, expect } from 'chai' import { OAuthMeteorModel } from '../lib/model/model' import { DefaultModelConfig } from '../lib/model/DefaultModelConfig' @@ -33,6 +33,13 @@ describe('model', function () { randomClientsName = Random.id() }) + afterEach(function () { + Mongo.Collection.get(DefaultModelConfig.clientsCollectionName).remove({}) + Mongo.Collection.get(DefaultModelConfig.accessTokensCollectionName).remove({}) + Mongo.Collection.get(DefaultModelConfig.refreshTokensCollectionName).remove({}) + Mongo.Collection.get(DefaultModelConfig.authCodesCollectionName).remove({}) + }) + describe('constructor', function () { it('can be created with defaults', function () { assert.isDefined(new OAuthMeteorModel()) @@ -154,13 +161,48 @@ describe('model', function () { }) describe('saveToken', function () { - it('saves an access token') - it('optionally saves a refresh token') - it('optionally allows to assign extended values') + let model + + beforeEach(function () { + model = new OAuthMeteorModel() + }) + + it('saves an access token', async () => { + const insertTokenDoc = { + accessToken: Random.id(), + accessTokenExpiresAt: new Date(), + refreshToken: Random.id(), + refreshTokenExpiresAt: new Date(), + scope: ['foo', 'bar'] + } + const clientDoc = { clientId: Random.id() } + const userDoc = { id: Random.id() } + const tokenDoc = await model.saveToken(insertTokenDoc, clientDoc, userDoc) + expect(tokenDoc).to.deep.equal({ + ...tokenDoc, + client: { id: clientDoc.clientId }, + user: userDoc + }) + }) }) describe('getAccessToken', function () { - it('returns a saved token') + let model + + beforeEach(function () { + model = new OAuthMeteorModel() + }) + + it('returns a saved token', async () => { + const collection = Mongo.Collection.get(DefaultModelConfig.accessTokensCollectionName) + const accessToken = Random.id() + const docId = collection.insert({ accessToken }) + const tokenDoc = await model.getAccessToken(accessToken) + expect(tokenDoc).to.deep.equal({ + _id: docId, + accessToken + }) + }) }) describe('saveAuthorizationCode', function () { diff --git a/tests/oauth-tests.js b/tests/oauth-tests.js index d5d4c26..c50a902 100644 --- a/tests/oauth-tests.js +++ b/tests/oauth-tests.js @@ -105,12 +105,13 @@ describe('integration tests of OAuth2 workflows', function () { }) } - const ClientCollection = Mongo.Collection.get(DefaultModelConfig.clientsCollectionName) + let ClientCollection let clientDoc let user - beforeEach(function () { - const clientDocId = authCodeServer.registerClient({ + beforeEach(async function () { + ClientCollection = Mongo.Collection.get(DefaultModelConfig.clientsCollectionName) + const clientDocId = await authCodeServer.registerClient({ title: Random.id(), redirectUris: [Meteor.absoluteUrl(`/${Random.id()}`)], grants: ['authorization_code'] From 7c241396cf0e2eac8a6d80e207c8ab126fc42436 Mon Sep 17 00:00:00 2001 From: jankapunkt Date: Tue, 2 Jul 2024 16:25:42 +0200 Subject: [PATCH 2/3] ci: split lint and tests for better reporting --- .github/workflows/tests.yml | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index de1cfbb..5c2a271 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -8,17 +8,43 @@ on: pull_request: jobs: + lint: + name: StandardJS lint + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v4 + - name: setup node + uses: actions/setup-node@v4 + with: + node-version: 20 + - name: Cache NPM dependencies + id: cache-meteor-npm + uses: actions/cache@v4 + with: + path: ~/.npm + key: v3-npm-${{ hashFiles('package-lock.json') }} + restore-keys: | + v3-npm- + + - name: Run lint + run: | + cd test-proxy + npm install + npm run setup + npm run test + tests: name: Meteor tests runs-on: ubuntu-latest steps: - name: checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 # CACHING - name: Install Meteor id: cache-meteor-install - uses: actions/cache@v3 + uses: actions/cache@v4 with: path: ~/.meteor key: v3-meteor-${{ hashFiles('.meteor/versions') }} @@ -52,10 +78,9 @@ jobs: with: meteor-release: '2.8.1' - - name: Run lint and tests + - name: Run tests run: | cd test-proxy meteor npm install meteor npm run setup - meteor npm run lint meteor npm run test From bee0d780cbb5a385ce0a6a91e3f016d399bf2169 Mon Sep 17 00:00:00 2001 From: jankapunkt Date: Tue, 2 Jul 2024 16:28:10 +0200 Subject: [PATCH 3/3] fix(ci): use correct lint command --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5c2a271..ceed3a9 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -19,7 +19,7 @@ jobs: with: node-version: 20 - name: Cache NPM dependencies - id: cache-meteor-npm + id: cache-npm uses: actions/cache@v4 with: path: ~/.npm @@ -32,7 +32,7 @@ jobs: cd test-proxy npm install npm run setup - npm run test + npm run lint tests: name: Meteor tests