-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for authentication for morpher, added .travis.yml, some s…
…mall fixes to context and auth on subscription
- Loading branch information
1 parent
817ef62
commit 96d9f02
Showing
16 changed files
with
367 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ../ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// }); | ||
// }); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, | ||
}, | ||
}, | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
import './morpher/morpher.test'; | ||
import './morpher/client'; | ||
import './accounts/client'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
Oops, something went wrong.