Skip to content

Commit

Permalink
Use Sinon sandbox (#1752)
Browse files Browse the repository at this point in the history
* use sinon sandbox

* don't restore spies individually
  • Loading branch information
marcoow authored Mar 23, 2019
1 parent 0f451a1 commit d72a350
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 30 deletions.
8 changes: 7 additions & 1 deletion tests/unit/authenticators/torii-test.js
Original file line number Diff line number Diff line change
@@ -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() {},
Expand All @@ -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() {
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/authorizers/devise-test.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/authorizers/oauth2-bearer-test.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/initializers/setup-session-restoration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
};
Expand All @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/initializers/setup-session-service-test.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/initializers/setup-session-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ 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() {}
};
Ember.testing = true;
});

afterEach(function() {
sinon.restore();
});

it('registers the session', function() {
sinon.spy(registry, 'register');
setupSession(registry);
Expand Down
16 changes: 8 additions & 8 deletions tests/unit/internal-session-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@ 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';

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();
session = createWithContainer(InternalSession, { store }, container);
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');
Expand Down Expand Up @@ -381,21 +387,15 @@ 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() {
let param = { some: 'random data' };
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() {
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/mixins/application-route-mixin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ 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';

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(),
Expand All @@ -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');
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/mixins/authenticated-route-mixin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 router;
Expand All @@ -20,6 +21,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({
Expand Down
8 changes: 7 additions & 1 deletion tests/unit/mixins/data-adapter-mixin-test.js
Original file line number Diff line number Diff line change
@@ -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() {},
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ 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() {
sinon = sinonjs.sandbox.create();
session = EmberObject.extend({
authenticate(authenticator, hash) {
if (!isEmpty(hash.access_token)) {
Expand All @@ -36,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
Expand Down
11 changes: 10 additions & 1 deletion tests/unit/mixins/unauthenticated-route-mixin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading

0 comments on commit d72a350

Please sign in to comment.