From 6a9a0faf23d5c31b5794807dc8091182166e1f62 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Fri, 22 Mar 2019 09:04:17 +0100 Subject: [PATCH 1/2] use sinon sandbox --- tests/unit/authenticators/torii-test.js | 8 +++++++- tests/unit/authorizers/devise-test.js | 8 +++++++- tests/unit/authorizers/oauth2-bearer-test.js | 8 +++++++- .../initializers/setup-session-restoration-test.js | 8 +++++++- tests/unit/initializers/setup-session-service-test.js | 8 +++++++- tests/unit/initializers/setup-session-test.js | 8 +++++++- tests/unit/internal-session-test.js | 8 +++++++- tests/unit/mixins/application-route-mixin-test.js | 8 +++++++- tests/unit/mixins/authenticated-route-mixin-test.js | 11 ++++++++++- tests/unit/mixins/data-adapter-mixin-test.js | 8 +++++++- ...oauth2-implicit-grant-callback-route-mixin-test.js | 9 +++++++++ tests/unit/services/session-test.js | 8 +++++++- tests/unit/session-stores/adaptive-test.js | 8 +++++++- tests/unit/session-stores/cookie-test.js | 8 +++++++- .../session-stores/shared/cookie-store-behavior.js | 7 ++++--- .../shared/storage-event-handler-behavior.js | 9 ++++++++- 16 files changed, 115 insertions(+), 17 deletions(-) diff --git a/tests/unit/authenticators/torii-test.js b/tests/unit/authenticators/torii-test.js index 08ea27949..955a31299 100644 --- a/tests/unit/authenticators/torii-test.js +++ b/tests/unit/authenticators/torii-test.js @@ -1,14 +1,16 @@ import RSVP from 'rsvp'; import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import Torii from 'ember-simple-auth/authenticators/torii'; describe('ToriiAuthenticator', () => { + let sinon; let authenticator; let torii; beforeEach(function() { + sinon = sinonjs.sandbox.create(); torii = { fetch() {}, open() {}, @@ -17,6 +19,10 @@ describe('ToriiAuthenticator', () => { authenticator = Torii.create({ torii }); }); + afterEach(function() { + sinon.restore(); + }); + describe('#restore', function() { function itDoesNotRestore(data) { it('returns a rejecting promise', function() { diff --git a/tests/unit/authorizers/devise-test.js b/tests/unit/authorizers/devise-test.js index 5705778fa..d93f9e64e 100644 --- a/tests/unit/authorizers/devise-test.js +++ b/tests/unit/authorizers/devise-test.js @@ -1,19 +1,25 @@ import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import Devise from 'ember-simple-auth/authorizers/devise'; import { registerDeprecationHandler } from '@ember/debug'; describe('DeviseAuthorizer', () => { + let sinon; let authorizer; let block; let data; beforeEach(function() { + sinon = sinonjs.sandbox.create(); authorizer = Devise.create(); block = sinon.spy(); }); + afterEach(function() { + sinon.restore(); + }); + it('shows deprecation warning from BaseAuthorizer', function() { let warnings; registerDeprecationHandler((message, options, next) => { diff --git a/tests/unit/authorizers/oauth2-bearer-test.js b/tests/unit/authorizers/oauth2-bearer-test.js index bb97e5922..e3ea7d3f8 100644 --- a/tests/unit/authorizers/oauth2-bearer-test.js +++ b/tests/unit/authorizers/oauth2-bearer-test.js @@ -1,18 +1,24 @@ import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import OAuth2BearerAuthorizer from 'ember-simple-auth/authorizers/oauth2-bearer'; import { registerDeprecationHandler } from '@ember/debug'; describe('OAuth2BearerAuthorizer', () => { + let sinon; let authorizer; let data; let block; beforeEach(function() { + sinon = sinonjs.sandbox.create(); block = sinon.spy(); }); + afterEach(function() { + sinon.restore(); + }); + it('shows deprecation warning from BaseAuthorizer', function() { let warnings; registerDeprecationHandler((message, options, next) => { diff --git a/tests/unit/initializers/setup-session-restoration-test.js b/tests/unit/initializers/setup-session-restoration-test.js index f0dc96bd0..53bc9eb35 100644 --- a/tests/unit/initializers/setup-session-restoration-test.js +++ b/tests/unit/initializers/setup-session-restoration-test.js @@ -3,15 +3,17 @@ import { getOwner, setOwner } from '@ember/application'; import RSVP from 'rsvp'; import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import setupSessionRestoration from 'ember-simple-auth/initializers/setup-session-restoration'; describe('setupSessionRestoration', () => { + let sinon; let registry; let resolveStub; let ApplicationRoute; beforeEach(function() { + sinon = sinonjs.sandbox.create(); registry = { resolve() {} }; @@ -21,6 +23,10 @@ describe('setupSessionRestoration', () => { resolveStub = sinon.stub(registry, 'resolve'); }); + afterEach(function() { + sinon.restore(); + }); + it('adds a beforeModel method', function() { resolveStub.withArgs('route:application').returns(ApplicationRoute); setupSessionRestoration(registry); diff --git a/tests/unit/initializers/setup-session-service-test.js b/tests/unit/initializers/setup-session-service-test.js index dbf13af84..cc1b72af7 100644 --- a/tests/unit/initializers/setup-session-service-test.js +++ b/tests/unit/initializers/setup-session-service-test.js @@ -1,17 +1,23 @@ import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import setupSessionService from 'ember-simple-auth/initializers/setup-session-service'; describe('setupSessionService', () => { + let sinon; let registry; beforeEach(function() { + sinon = sinonjs.sandbox.create(); registry = { injection() {} }; }); + afterEach(function() { + sinon.restore(); + }); + it('injects the session into the session service', function() { sinon.spy(registry, 'injection'); setupSessionService(registry); diff --git a/tests/unit/initializers/setup-session-test.js b/tests/unit/initializers/setup-session-test.js index 71b9741a5..ea664b03c 100644 --- a/tests/unit/initializers/setup-session-test.js +++ b/tests/unit/initializers/setup-session-test.js @@ -6,15 +6,17 @@ import { it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import setupSession from 'ember-simple-auth/initializers/setup-session'; import Ephemeral from 'ember-simple-auth/session-stores/ephemeral'; import InternalSession from 'ember-simple-auth/internal-session'; describe('setupSession', () => { + let sinon; let registry; beforeEach(function() { + sinon = sinonjs.sandbox.create(); registry = { register() {}, injection() {} @@ -22,6 +24,10 @@ describe('setupSession', () => { Ember.testing = true; }); + afterEach(function() { + sinon.restore(); + }); + it('registers the session', function() { sinon.spy(registry, 'register'); setupSession(registry); diff --git a/tests/unit/internal-session-test.js b/tests/unit/internal-session-test.js index 35708441b..a920c5736 100644 --- a/tests/unit/internal-session-test.js +++ b/tests/unit/internal-session-test.js @@ -2,7 +2,7 @@ import RSVP from 'rsvp'; import { next } from '@ember/runloop'; import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import InternalSession from 'ember-simple-auth/internal-session'; import EphemeralStore from 'ember-simple-auth/session-stores/ephemeral'; import Authenticator from 'ember-simple-auth/authenticators/base'; @@ -10,12 +10,14 @@ import Authenticator from 'ember-simple-auth/authenticators/base'; import createWithContainer from '../helpers/create-with-container'; describe('InternalSession', () => { + let sinon; let session; let store; let authenticator; let container; beforeEach(function() { + sinon = sinonjs.sandbox.create(); container = { lookup() {} }; store = EphemeralStore.create(); authenticator = Authenticator.create(); @@ -23,6 +25,10 @@ describe('InternalSession', () => { sinon.stub(container, 'lookup').withArgs('authenticator').returns(authenticator); }); + afterEach(function() { + sinon.restore(); + }); + it('does not allow data to be stored for the key "authenticated"', function() { expect(() => { session.set('authenticated', 'test'); diff --git a/tests/unit/mixins/application-route-mixin-test.js b/tests/unit/mixins/application-route-mixin-test.js index 8dddfc93b..782bbdda1 100644 --- a/tests/unit/mixins/application-route-mixin-test.js +++ b/tests/unit/mixins/application-route-mixin-test.js @@ -2,7 +2,7 @@ import Route from '@ember/routing/route'; import { next } from '@ember/runloop'; import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin'; import InternalSession from 'ember-simple-auth/internal-session'; import EphemeralStore from 'ember-simple-auth/session-stores/ephemeral'; @@ -10,12 +10,14 @@ import EphemeralStore from 'ember-simple-auth/session-stores/ephemeral'; import createWithContainer from '../../helpers/create-with-container'; describe('ApplicationRouteMixin', () => { + let sinon; let session; let route; let cookiesMock; let containerMock; beforeEach(function() { + sinon = sinonjs.sandbox.create(); session = InternalSession.create({ store: EphemeralStore.create() }); cookiesMock = { read: sinon.stub(), @@ -32,6 +34,10 @@ describe('ApplicationRouteMixin', () => { }), { session }, containerMock); }); + afterEach(function() { + sinon.restore(); + }); + describe('mapping of service events to route methods', function() { beforeEach(function() { sinon.spy(route, 'sessionAuthenticated'); diff --git a/tests/unit/mixins/authenticated-route-mixin-test.js b/tests/unit/mixins/authenticated-route-mixin-test.js index c046e7e9c..f8ee3980e 100644 --- a/tests/unit/mixins/authenticated-route-mixin-test.js +++ b/tests/unit/mixins/authenticated-route-mixin-test.js @@ -3,7 +3,7 @@ import RSVP from 'rsvp'; import Route from '@ember/routing/route'; import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin'; import InternalSession from 'ember-simple-auth/internal-session'; import Configuration from 'ember-simple-auth/configuration'; @@ -12,6 +12,7 @@ import EphemeralStore from 'ember-simple-auth/session-stores/ephemeral'; import createWithContainer from '../../helpers/create-with-container'; describe('AuthenticatedRouteMixin', () => { + let sinon; let route; let session; let transition; @@ -19,6 +20,14 @@ describe('AuthenticatedRouteMixin', () => { let fastbootMock; let containerMock; + beforeEach(function() { + sinon = sinonjs.sandbox.create(); + }); + + afterEach(function() { + sinon.restore(); + }); + describe('#beforeModel', function() { beforeEach(function() { const MixinImplementingBeforeModel = Mixin.create({ diff --git a/tests/unit/mixins/data-adapter-mixin-test.js b/tests/unit/mixins/data-adapter-mixin-test.js index d2b8bd486..e1bba074b 100644 --- a/tests/unit/mixins/data-adapter-mixin-test.js +++ b/tests/unit/mixins/data-adapter-mixin-test.js @@ -1,16 +1,18 @@ import EmberObject from '@ember/object'; import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin'; describe('DataAdapterMixin', () => { + let sinon; let adapter; let sessionService; let hash; let Adapter; beforeEach(function() { + sinon = sinonjs.sandbox.create(); hash = {}; sessionService = EmberObject.create({ authorize() {}, @@ -36,6 +38,10 @@ describe('DataAdapterMixin', () => { adapter = Adapter.create({ session: sessionService }); }); + afterEach(function() { + sinon.restore(); + }); + describe('#ajaxOptions', function() { it('registers a beforeSend hook', function() { adapter.ajaxOptions(); diff --git a/tests/unit/mixins/oauth2-implicit-grant-callback-route-mixin-test.js b/tests/unit/mixins/oauth2-implicit-grant-callback-route-mixin-test.js index ccf231eac..0a180e214 100644 --- a/tests/unit/mixins/oauth2-implicit-grant-callback-route-mixin-test.js +++ b/tests/unit/mixins/oauth2-implicit-grant-callback-route-mixin-test.js @@ -10,9 +10,18 @@ import OAuth2ImplicitGrantCallbackRouteMixin from 'ember-simple-auth/mixins/oaut import * as LocationUtil from 'ember-simple-auth/utils/location'; describe('OAuth2ImplicitGrantCallbackRouteMixin', function() { + let sinon; let route; let session; + beforeEach(function() { + sinon = sinonjs.sandbox.create(); + }); + + afterEach(function() { + sinon.restore(); + }); + describe('#activate', function() { let sinon; beforeEach(function() { diff --git a/tests/unit/services/session-test.js b/tests/unit/services/session-test.js index 5c6dd400c..a57c54c18 100644 --- a/tests/unit/services/session-test.js +++ b/tests/unit/services/session-test.js @@ -5,17 +5,19 @@ import { set } from '@ember/object'; import { registerDeprecationHandler } from '@ember/debug'; import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import Session from 'ember-simple-auth/services/session'; import createWithContainer from '../../helpers/create-with-container'; describe('SessionService', () => { + let sinon; let sessionService; let session; let authorizer; beforeEach(function() { + sinon = sinonjs.sandbox.create(); session = ObjectProxy.extend(Evented, { content: {} }).create(); @@ -29,6 +31,10 @@ describe('SessionService', () => { sessionService = createWithContainer(Session, { session }, container); }); + afterEach(function() { + sinon.restore(); + }); + it('forwards the "authenticationSucceeded" event from the session', function(done) { let triggered = false; sessionService.one('authenticationSucceeded', () => (triggered = true)); diff --git a/tests/unit/session-stores/adaptive-test.js b/tests/unit/session-stores/adaptive-test.js index 4ef0f8e89..61c5d6612 100644 --- a/tests/unit/session-stores/adaptive-test.js +++ b/tests/unit/session-stores/adaptive-test.js @@ -6,7 +6,7 @@ import { it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import Adaptive from 'ember-simple-auth/session-stores/adaptive'; import LocalStorage from 'ember-simple-auth/session-stores/local-storage'; import itBehavesLikeAStore from './shared/store-behavior'; @@ -15,10 +15,16 @@ import FakeCookieService from '../../helpers/fake-cookie-service'; import createAdaptiveStore from '../../helpers/create-adaptive-store'; describe('AdaptiveStore', () => { + let sinon; let store; + beforeEach(function() { + sinon = sinonjs.sandbox.create(); + }); + afterEach(function() { store.clear(); + sinon.restore(); }); describe('when localStorage is available', function() { diff --git a/tests/unit/session-stores/cookie-test.js b/tests/unit/session-stores/cookie-test.js index 36aace533..e6bf610f1 100644 --- a/tests/unit/session-stores/cookie-test.js +++ b/tests/unit/session-stores/cookie-test.js @@ -1,17 +1,23 @@ import { describe, beforeEach } from 'mocha'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import itBehavesLikeAStore from './shared/store-behavior'; import itBehavesLikeACookieStore from './shared/cookie-store-behavior'; import FakeCookieService from '../../helpers/fake-cookie-service'; import createCookieStore from '../../helpers/create-cookie-store'; describe('CookieStore', () => { + let sinon; let store; beforeEach(function() { + sinon = sinonjs.sandbox.create(); store = createCookieStore(FakeCookieService.create()); }); + afterEach(function() { + sinon.restore(); + }); + itBehavesLikeAStore({ store() { return store; diff --git a/tests/unit/session-stores/shared/cookie-store-behavior.js b/tests/unit/session-stores/shared/cookie-store-behavior.js index 32b5fe73b..f49ceb6ff 100644 --- a/tests/unit/session-stores/shared/cookie-store-behavior.js +++ b/tests/unit/session-stores/shared/cookie-store-behavior.js @@ -7,7 +7,7 @@ import { it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import FakeCookieService from '../../../helpers/fake-cookie-service'; let warnings; @@ -22,6 +22,7 @@ registerWarnHandler((message, options, next) => { }); export default function(options) { + let sinon; let store; let createStore; let renew; @@ -31,6 +32,7 @@ export default function(options) { // eslint-disable-next-line mocha/no-top-level-hooks beforeEach(function() { + sinon = sinonjs.sandbox.create(); createStore = options.createStore; renew = options.renew; sync = options.sync; @@ -43,8 +45,7 @@ export default function(options) { // eslint-disable-next-line mocha/no-top-level-hooks afterEach(function() { - cookieService.read.restore(); - cookieService.write.restore(); + sinon.restore(); store.clear(); }); diff --git a/tests/unit/session-stores/shared/storage-event-handler-behavior.js b/tests/unit/session-stores/shared/storage-event-handler-behavior.js index 3e1f68bc5..9720f6439 100644 --- a/tests/unit/session-stores/shared/storage-event-handler-behavior.js +++ b/tests/unit/session-stores/shared/storage-event-handler-behavior.js @@ -1,16 +1,23 @@ import { run } from '@ember/runloop'; import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; export default function(options) { + let sinon; let store; // eslint-disable-next-line mocha/no-top-level-hooks beforeEach(function() { + sinon = sinonjs.sandbox.create(); store = options.store(); }); + // eslint-disable-next-line mocha/no-top-level-hooks + afterEach(function() { + sinon.restore(); + }); + describe('storage events', function() { beforeEach(function() { sinon.spy(window, 'addEventListener'); From 30b1041a82808d6bc552da2613c2d567da571d31 Mon Sep 17 00:00:00 2001 From: Marco Otte-Witte Date: Fri, 22 Mar 2019 09:05:39 +0100 Subject: [PATCH 2/2] don't restore spies individually --- tests/unit/internal-session-test.js | 8 +------- ...oauth2-implicit-grant-callback-route-mixin-test.js | 5 ----- tests/unit/mixins/unauthenticated-route-mixin-test.js | 11 ++++++++++- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/unit/internal-session-test.js b/tests/unit/internal-session-test.js index a920c5736..0ef729b78 100644 --- a/tests/unit/internal-session-test.js +++ b/tests/unit/internal-session-test.js @@ -387,10 +387,8 @@ describe('InternalSession', () => { }); describe('when invalidate gets called with additional params', function() { - let spy; - beforeEach(function() { - spy = sinon.spy(authenticator, 'invalidate'); + sinon.spy(authenticator, 'invalidate'); }); it('passes the params on to the authenticators invalidate method', function() { @@ -398,10 +396,6 @@ describe('InternalSession', () => { session.invalidate(param); expect(authenticator.invalidate).to.have.been.calledWith(session.get('authenticated'), param); }); - - afterEach(function() { - spy.restore(); - }); }); describe('when the authenticator resolves invalidation', function() { diff --git a/tests/unit/mixins/oauth2-implicit-grant-callback-route-mixin-test.js b/tests/unit/mixins/oauth2-implicit-grant-callback-route-mixin-test.js index 0a180e214..ffedf4c0e 100644 --- a/tests/unit/mixins/oauth2-implicit-grant-callback-route-mixin-test.js +++ b/tests/unit/mixins/oauth2-implicit-grant-callback-route-mixin-test.js @@ -23,9 +23,7 @@ describe('OAuth2ImplicitGrantCallbackRouteMixin', function() { }); describe('#activate', function() { - let sinon; beforeEach(function() { - sinon = sinonjs.sandbox.create(); session = EmberObject.extend({ authenticate(authenticator, hash) { if (!isEmpty(hash.access_token)) { @@ -45,9 +43,6 @@ describe('OAuth2ImplicitGrantCallbackRouteMixin', function() { sinon.spy(route, 'transitionTo'); }); - afterEach(function() { - sinon.restore(); - }); it('correctly passes the auth parameters if authentication succeeds', function(done) { // it isn't possible to stub window.location.hash so we stub a wrapper function instead diff --git a/tests/unit/mixins/unauthenticated-route-mixin-test.js b/tests/unit/mixins/unauthenticated-route-mixin-test.js index 44679b2a1..091d2d628 100644 --- a/tests/unit/mixins/unauthenticated-route-mixin-test.js +++ b/tests/unit/mixins/unauthenticated-route-mixin-test.js @@ -3,17 +3,26 @@ import RSVP from 'rsvp'; import Route from '@ember/routing/route'; import { describe, beforeEach, it } from 'mocha'; import { expect } from 'chai'; -import sinon from 'sinon'; +import sinonjs from 'sinon'; import UnauthenticatedRouteMixin from 'ember-simple-auth/mixins/unauthenticated-route-mixin'; import InternalSession from 'ember-simple-auth/internal-session'; import EphemeralStore from 'ember-simple-auth/session-stores/ephemeral'; import createWithContainer from '../../helpers/create-with-container'; describe('UnauthenticatedRouteMixin', () => { + let sinon; let route; let session; let containerMock; + beforeEach(function() { + sinon = sinonjs.sandbox.create(); + }); + + afterEach(function() { + sinon.restore(); + }); + describe('#beforeModel', function() { beforeEach(function() { const MixinImplementingBeforeModel = Mixin.create({