Skip to content

Commit

Permalink
Added tests for authentication for morpher, added .travis.yml, some s…
Browse files Browse the repository at this point in the history
…mall fixes to context and auth on subscription
  • Loading branch information
theodorDiaconu committed Jul 23, 2018
1 parent 817ef62 commit 96d9f02
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 156 deletions.
31 changes: 31 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
sudo: required
addons:
chrome: stable

language: node_js

node_js:
- "8.9.1"

before_install:
- curl https://install.meteor.com | /bin/sh
- export PATH="$HOME/.meteor:$PATH"

cache:
directories:
- ~/.npm
- "node_modules"

notifications:
email: false

before_script:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- sleep 3

script:
- meteor create --bare test
- cd test
- meteor npm i --save selenium-webdriver@3.6.0 chromedriver@2.36.0 simpl-schema
- METEOR_PACKAGE_DIRS="../" TEST_BROWSER_DRIVER=chrome meteor test-packages --once --driver-package meteortesting:mocha ../
92 changes: 92 additions & 0 deletions __tests__/accounts/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import client, { wsLink } from '../apolloClient';
import gql from 'graphql-tag';
import { loginWithPassword, logout } from 'meteor-apollo-accounts';
import { resolve } from 'dns';

const PASSWORD = '12345';

describe('Accounts', () => {
it('Should allow me to login', async () => {
const username = 'account-1';
const userId = await loginWithPassword(
{ username, password: PASSWORD },
client
);

assert.isString(userId);
await logout(client);
});

it('Should throw an error if invalid login', done => {
const username = 'accountZ-2';
loginWithPassword({ username, password: PASSWORD }, client).catch(e => {
done();
});
});

it('Should properly store the token in such a way that I am identified', async () => {
const username = 'account-1';
const userId = await loginWithPassword(
{ username, password: PASSWORD },
client
);

const query = gql`
query {
me {
_id
}
}
`;

const response = await client.query({
query,
});

const { me } = response.data;

assert.equal(userId, me._id);
await logout(client);
});

// TODO: It seems that the subscription is authenticated properly
// However, due to the nature of how subscriptions work, if a new message is received before asyncIterator is returned
// That message doesn't reach the client

// it('Should identify subscription authentication', async () => {
// const username = 'account-1';
// const userId = await loginWithPassword(
// { username, password: PASSWORD },
// client
// );

// assert.isString(userId);
// wsLink.subscriptionClient.close(true);

// return new Promise((resolve, reject) => {
// setTimeout(() => {
// const query = gql`
// subscription {
// me {
// event
// doc
// }
// }
// `;

// const observable = client.subscribe({ query });
// const subscription = observable.subscribe(
// x => {
// console.log(x);
// },
// () => {
// console.log('err');
// },
// () => {
// console.log('Finished');
// }
// );
// }, 500);
// });
// });
});
66 changes: 66 additions & 0 deletions __tests__/accounts/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { load } from 'meteor/cultofcoders:apollo';
import { asyncIterator } from 'apollo-live-server';

const PASSWORD = '12345';

Meteor.users.remove({});

Accounts.createUser({
username: 'account-1',
email: 'account-1@apollo.com',
password: PASSWORD,
});

Accounts.createUser({
username: 'account-2',
email: 'account-2@apollo.com',
password: PASSWORD,
});

load({
typeDefs: `
type Query {
me: User
}
type Subscription {
me: SubscriptionEvent
}
`,
resolvers: {
Query: {
me(_, args, { userId }) {
if (!userId) {
throw new Error('not-allowed');
}

return Meteor.users.findOne(userId);
},
},
Subscription: {
me: {
resolve: payload => payload,
subscribe(_, args, ctx) {
if (!ctx.userId) {
throw new Error('not-allowed');
}

const iterator = asyncIterator(
Meteor.users.find({
_id: ctx.userId,
}),
{
sendInitialAdds: true,
}
);

console.log('Finish iterator');

return iterator;
},
},
},
},
});
7 changes: 7 additions & 0 deletions __tests__/apolloClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { initialize } from 'meteor/cultofcoders:apollo';

const { client, wsLink } = initialize();

export { wsLink };

export default client;
3 changes: 2 additions & 1 deletion __tests__/client.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './morpher/morpher.test';
import './morpher/client';
import './accounts/client';
11 changes: 11 additions & 0 deletions __tests__/graphql/accounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { initAccounts } from 'meteor/cultofcoders:apollo-accounts';

// Load all accounts related resolvers and type definitions into graphql-loader
const AccountsModule = initAccounts({
loginWithFacebook: false,
loginWithGoogle: false,
loginWithLinkedIn: false,
loginWithPassword: true,
}); // returns { typeDefs, resolvers }

export default AccountsModule;
8 changes: 6 additions & 2 deletions __tests__/graphql/init.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { load } from 'meteor/cultofcoders:apollo';
import { typeDefs } from './module';
import MorpherModule from './morpher';
import AccountsModule from './accounts';

load({ typeDefs });
load([
AccountsModule,
MorpherModule
]);
File renamed without changes.
137 changes: 132 additions & 5 deletions __tests__/morpher/client.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,135 @@
import { initialize } from 'meteor/cultofcoders:apollo';
import { setClient } from 'apollo-morpher';
import db, { setClient } from 'apollo-morpher';
import client from '../apolloClient';
setClient(client);

const { client } = initialize();
describe('Morpher', () => {
it('Should work with insert()', done => {
db.users
.insert({
firstName: 'John',
lastName: 'Smith',
})
.then(result => {
assert.isString(result._id);
done();
});
});
it('Should work with update()', async () => {
const { _id } = await db.users.insert({
firstName: 'John',
lastName: 'Smith',
});

setClient(client);
const response = await db.users.update(
{ _id },
{
$set: { lastName: 'Brown' },
}
);

assert.equal('ok', response);
});

it('Should work with remove()', async () => {
const { _id } = await db.users.insert({
firstName: 'John',
lastName: 'Smith',
});

const response = await db.users.remove({ _id });

assert.equal('ok', response);
});

it('Should work with find()', async () => {
const { _id } = await db.users.insert({
firstName: 'John',
lastName: 'Smith',
});

const users = await db.users.find(
{
_id: 1,
firstName: 1,
lastName: 1,
},
{
filters: { _id },
}
);

assert.lengthOf(users, 1);
assert.isObject(users[0]);
assert.equal(_id, users[0]._id);
assert.equal('John', users[0].firstName);
assert.equal('Smith', users[0].lastName);
});

it('Should work with findOne()', async () => {
const { _id } = await db.users.insert({
firstName: 'John',
lastName: 'Smith',
});

const user = await db.users.findOne(
{
_id: 1,
firstName: 1,
lastName: 1,
},
{
filters: { _id },
}
);

assert.isObject(user);
assert.equal(_id, user._id);
assert.equal('John', user.firstName);
assert.equal('Smith', user.lastName);
});

it('Should work with count()', async () => {
await db.users.remove({
firstName: 'JohnCountable',
});

const d1 = await db.users.insert({
firstName: 'JohnCountable',
});
const d2 = await db.users.insert({
firstName: 'JohnCountable',
});

const count = await db.users.count({
filters: {
firstName: 'JohnCountable',
},
});

assert.equal(2, count);
});

it('Should work with options', async () => {
await db.users.remove({
firstName: 'JohnOptionable',
});

const d1 = await db.users.insert({
firstName: 'JohnOptionable',
});
const d2 = await db.users.insert({
firstName: 'JohnOptionable',
});

const results = await db.users.find(`firstName`, {
filters: {
firstName: 'JohnOptionable',
},
options: {
limit: 1,
},
});

export default client;
assert.equal(1, results.length);
});
});
Loading

0 comments on commit 96d9f02

Please sign in to comment.