-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- inform user about the functionality being inactive on server - client side unit tests
- Loading branch information
Tomasz Przytuła
committed
Feb 4, 2018
1 parent
7bd9296
commit b0ff369
Showing
8 changed files
with
353 additions
and
25 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
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,34 +1,38 @@ | ||
import { Accounts } from 'meteor/accounts-base'; | ||
import overrideAccountsLogin from './overrideLogin'; | ||
overrideAccountsLogin(); | ||
|
||
const RememberMe = {}; | ||
const updateRememberMe = 'tprzytula:rememberMe-update'; | ||
|
||
RememberMe.loginWithPassword = (user, password, callback = () => {}, rememberMe = true) => { | ||
let flag = rememberMe; | ||
let callbackMethod = () => {}; | ||
const flag = (typeof callback === 'boolean') | ||
? callback | ||
: rememberMe; | ||
|
||
if (typeof callback === 'boolean') { | ||
flag = callback; | ||
} else { | ||
callbackMethod = callback; | ||
} | ||
const callbackMethod = (typeof callback === 'function') | ||
? callback | ||
: () => {}; | ||
|
||
Meteor.loginWithPassword(user, password, (error) => { | ||
if (!error) { | ||
Meteor.call(updateRememberMe, flag); | ||
Meteor.call(updateRememberMe, flag, (error) => { | ||
if (error && error.error === 404) { | ||
console.warn( | ||
'Dependency meteor/tprzytula:remember-me is not active!\n', | ||
'\nTo activate it make sure to run "RememberMe.activate()" on the server.' + | ||
'It is required to be able to access the functionality on the client.' | ||
) | ||
} else if (error) { | ||
console.error( | ||
'meteor/tprzytula:remember-me' + | ||
'\nCould not update remember me setting.' + | ||
'\nError:', error | ||
); | ||
} | ||
}); | ||
} | ||
callbackMethod(error); | ||
}); | ||
}; | ||
|
||
let loginOverridden = false; | ||
Accounts.onLogin(() => { | ||
/* Override meteor accounts callLoginMethod to store information that user logged before */ | ||
if (!loginOverridden) { | ||
overrideAccountsLogin(); | ||
loginOverridden = true; | ||
} | ||
}); | ||
|
||
export default RememberMe; |
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
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
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,6 +1,6 @@ | ||
{ | ||
"name": "rememberMe", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Extension for Meteor account-base package with the implementation of rememberMe", | ||
"license": "MIT", | ||
"author": "Tomasz Przytuła <[email protected]>", | ||
|
@@ -16,6 +16,7 @@ | |
], | ||
"homepage": "https://github.com/tprzytulacc/Meteor-Remember-Me", | ||
"scripts": { | ||
"test": "meteor test-packages ./ --driver-package meteortesting:mocha" | ||
"test": "meteor test-packages ./ --once --driver-package meteortesting:mocha", | ||
"test-watch": "TEST_WATCH=1 meteor test-packages ./ --full-app --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,10 @@ | ||
const method = require('./tests/method'); | ||
const rememberMe = require('./tests/rememberMe'); | ||
|
||
/** | ||
* Client-side test cases. | ||
*/ | ||
describe('client', () => { | ||
method(); | ||
rememberMe(); | ||
}); |
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,120 @@ | ||
const RememberMe = require('meteor/tprzytula:remember-me').default; | ||
const rememberMeMethod = 'tprzytula:rememberMe-update'; | ||
const sinon = require('sinon'); | ||
|
||
module.exports = () => { | ||
/** | ||
* RememberMe.loginWithPassword is a wrapper for the Meteor's | ||
* loginWithPassword method. It should match specific behaviour. | ||
*/ | ||
describe('Invoking "RememberMe.loginWithPassword" method', () => { | ||
/** | ||
* Each time the wrapper is called it | ||
* should also internally call the Meteor's login method. | ||
*/ | ||
it('should call Meteor.loginWithPassword', () => { | ||
const loginWithPassword = sinon.stub( | ||
Meteor, | ||
'loginWithPassword' | ||
); | ||
|
||
RememberMe.loginWithPassword('username', 'password'); | ||
expect(loginWithPassword).to.have.been.calledOnce; | ||
expect(loginWithPassword).to.have.been.calledWith( | ||
'username', | ||
'password' | ||
); | ||
|
||
RememberMe.loginWithPassword('username_two', 'password_two'); | ||
expect(loginWithPassword).to.have.been.calledTwice; | ||
expect(loginWithPassword).to.have.been.calledWith( | ||
'username_two', | ||
'password_two' | ||
); | ||
|
||
loginWithPassword.restore(); | ||
}); | ||
|
||
/** | ||
* Updating the Remember Me status is only relevant if the login | ||
* performed successfully. Otherwise there is no reason to send | ||
* this request to the server. | ||
*/ | ||
it('should not call updateRememberMe method if login failed', () => { | ||
const loginWithPassword = sinon.stub(Meteor, 'loginWithPassword'); | ||
const call = sinon.stub(Meteor, 'call'); | ||
loginWithPassword.callsFake((user, password, callback) => { | ||
const error = 'Invalid user'; | ||
callback(error); | ||
}); | ||
RememberMe.loginWithPassword('username', 'password'); | ||
expect(loginWithPassword).to.have.been.calledOnce; | ||
expect(call).to.have.callCount(0); | ||
|
||
loginWithPassword.restore(); | ||
call.restore(); | ||
}); | ||
|
||
/** | ||
* If the login performed successfully then method for updating | ||
* the state of remember me should be invoked. This way server | ||
* will be informed about requested change for this setting. | ||
*/ | ||
it('should call updateRememberMe method if logged in successfully', () => { | ||
const loginWithPassword = sinon.stub( | ||
Meteor, | ||
'loginWithPassword' | ||
); | ||
const call = sinon.stub(Meteor, 'call'); | ||
loginWithPassword.callsFake((user, password, callback) => { | ||
const error = false; | ||
callback(error); | ||
}); | ||
RememberMe.loginWithPassword('username', 'password'); | ||
expect(loginWithPassword).to.have.been.calledOnce; | ||
expect(loginWithPassword).to.have.been.calledWith( | ||
'username', | ||
'password' | ||
); | ||
expect(call).to.have.been.calledOnce; | ||
expect(call).to.have.been.calledWith(rememberMeMethod); | ||
|
||
loginWithPassword.restore(); | ||
call.restore(); | ||
}); | ||
|
||
/** | ||
* Meteor.loginWithPassword takes a callback as a parameter. | ||
* The callback provided to the RememberMe.loginWithPassword | ||
* should be invoked after received response from the Meteor's | ||
* internal login method. It should also sent the error as | ||
* a parameter (if encountered). | ||
*/ | ||
it('should correctly pass callback to "Meteor.loginWithPassword"', () => { | ||
const loginWithPassword = sinon.stub( | ||
Meteor, | ||
'loginWithPassword' | ||
); | ||
const call = sinon.stub(Meteor, 'call'); | ||
const error = 'User does not exist'; | ||
loginWithPassword.callsFake((user, password, callback) => { | ||
callback(error); | ||
}); | ||
const obj = {}; | ||
obj.callback = (error) => error; | ||
const callbackSpy = sinon.spy(obj, 'callback'); | ||
RememberMe.loginWithPassword('username', 'password', obj.callback); | ||
expect(loginWithPassword).to.have.been.calledOnce; | ||
expect(loginWithPassword).to.have.been.calledWith( | ||
'username', | ||
'password' | ||
); | ||
|
||
expect(callbackSpy).to.have.been.calledOnce; | ||
expect(callbackSpy).to.have.been.calledWith(error); | ||
|
||
loginWithPassword.restore(); | ||
call.restore(); | ||
}); | ||
}); | ||
}; |
Oops, something went wrong.