2016-07-30 16:14:43 +05:30
|
|
|
import expect from 'unexpected';
|
|
|
|
|
2016-06-10 10:36:21 +05:30
|
|
|
import AuthFlow from 'services/authFlow/AuthFlow';
|
|
|
|
|
|
|
|
import RegisterState from 'services/authFlow/RegisterState';
|
|
|
|
import ActivationState from 'services/authFlow/ActivationState';
|
|
|
|
import ResendActivationState from 'services/authFlow/ResendActivationState';
|
|
|
|
|
|
|
|
describe('AuthFlow.functional', () => {
|
|
|
|
let flow;
|
|
|
|
let actions;
|
|
|
|
let store;
|
|
|
|
let state;
|
|
|
|
let navigate;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2016-07-30 16:14:43 +05:30
|
|
|
actions = {};
|
2016-06-10 10:36:21 +05:30
|
|
|
store = {
|
2016-07-30 16:14:43 +05:30
|
|
|
getState: sinon.stub().named('store.getState'),
|
2016-06-10 10:36:21 +05:30
|
|
|
dispatch: sinon.spy(({type, payload = {}}) => {
|
|
|
|
if (type === '@@router/TRANSITION' && payload.method === 'push') {
|
|
|
|
// emulate redux-router
|
2016-07-30 16:14:43 +05:30
|
|
|
navigate(...payload.args);
|
2016-06-10 10:36:21 +05:30
|
|
|
}
|
2016-07-30 16:14:43 +05:30
|
|
|
}).named('store.dispatch')
|
2016-06-10 10:36:21 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
state = {};
|
|
|
|
|
|
|
|
flow = new AuthFlow(actions);
|
|
|
|
flow.setStore(store);
|
|
|
|
|
2016-08-07 19:24:59 +05:30
|
|
|
navigate = function navigate(path, extra = {}) { // emulates router behaviour
|
|
|
|
if (navigate.lastUrl !== path) {
|
|
|
|
navigate.lastUrl = path;
|
2016-08-07 20:19:26 +05:30
|
|
|
flow.handleRequest({path, query: {}, params: {}, ...extra}, navigate);
|
2016-06-10 10:36:21 +05:30
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-30 16:14:43 +05:30
|
|
|
sinon.stub(flow, 'run').named('flow.run');
|
|
|
|
sinon.spy(flow, 'navigate').named('flow.navigate');
|
2016-06-10 10:36:21 +05:30
|
|
|
store.getState.returns(state);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('guest', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
state.user = {
|
|
|
|
isGuest: true
|
|
|
|
};
|
2016-11-13 02:01:44 +05:30
|
|
|
state.auth = {
|
|
|
|
login: null
|
|
|
|
};
|
2016-06-10 10:36:21 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should redirect guest / -> /login', () => {
|
|
|
|
navigate('/');
|
|
|
|
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(flow.navigate, 'was called once');
|
|
|
|
expect(flow.navigate, 'to have a call satisfying', ['/login']);
|
2016-06-10 10:36:21 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should redirect guest to /login after /login -> /', () => {
|
|
|
|
// this is to ensure, that when AuthFlow is already on LoginState (on /login)
|
|
|
|
// it will not allow user to go to / (which is forbidden for users) and will
|
|
|
|
// always redirect to /login, so that enter condition of state is always satisfied
|
|
|
|
|
|
|
|
navigate('/login');
|
|
|
|
navigate('/');
|
|
|
|
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(flow.navigate, 'was called twice');
|
|
|
|
expect(flow.navigate, 'to have a call satisfying', ['/login']);
|
2016-06-10 10:36:21 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2016-08-07 20:19:26 +05:30
|
|
|
describe('oauth', () => {
|
|
|
|
it('should oauth without any rendering if no acceptance required', () => {
|
|
|
|
const expectedRedirect = 'foo';
|
2016-06-10 10:36:21 +05:30
|
|
|
|
2016-08-07 20:19:26 +05:30
|
|
|
Object.assign(state, {
|
|
|
|
user: {
|
|
|
|
isGuest: false,
|
|
|
|
isActive: true
|
|
|
|
},
|
2016-06-10 10:36:21 +05:30
|
|
|
|
2016-08-07 20:19:26 +05:30
|
|
|
auth: {
|
|
|
|
oauth: {
|
2016-11-19 20:11:15 +05:30
|
|
|
clientId: 123,
|
|
|
|
prompt: []
|
2016-06-10 10:36:21 +05:30
|
|
|
}
|
|
|
|
}
|
2016-08-07 20:19:26 +05:30
|
|
|
});
|
2016-06-10 10:36:21 +05:30
|
|
|
|
2016-08-07 20:19:26 +05:30
|
|
|
flow.run.onCall(0).returns({then: (fn) => fn()});
|
|
|
|
flow.run.onCall(1).returns({then: (fn) => fn({
|
|
|
|
redirectUri: expectedRedirect
|
|
|
|
})});
|
2016-06-10 10:36:21 +05:30
|
|
|
|
2016-08-07 20:19:26 +05:30
|
|
|
navigate('/oauth2');
|
2016-06-10 10:36:21 +05:30
|
|
|
|
2016-08-07 20:19:26 +05:30
|
|
|
expect(flow.run, 'to have calls satisfying', [
|
|
|
|
['oAuthValidate', {}],
|
|
|
|
['oAuthComplete', {}],
|
|
|
|
['redirect', expectedRedirect]
|
|
|
|
]);
|
|
|
|
});
|
2016-06-10 10:36:21 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
describe('/resend-activation #goBack()', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
state.user = {
|
|
|
|
isGuest: true,
|
|
|
|
isActive: false
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should goBack to /activation', () => {
|
|
|
|
navigate('/activation');
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(flow.state, 'to be a', ActivationState);
|
2016-06-10 10:36:21 +05:30
|
|
|
|
|
|
|
flow.state.reject(flow);
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(flow.state, 'to be a', ResendActivationState);
|
2016-06-10 10:36:21 +05:30
|
|
|
|
|
|
|
flow.state.goBack(flow);
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(flow.state, 'to be a', ActivationState);
|
2016-06-10 10:36:21 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('should goBack to /register', () => {
|
|
|
|
navigate('/register');
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(flow.state, 'to be a', RegisterState);
|
2016-06-10 10:36:21 +05:30
|
|
|
|
2016-07-24 14:02:54 +05:30
|
|
|
flow.state.reject(flow, {requestEmail: true});
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(flow.state, 'to be a', ResendActivationState);
|
2016-06-10 10:36:21 +05:30
|
|
|
|
|
|
|
flow.state.goBack(flow);
|
2016-07-30 16:14:43 +05:30
|
|
|
expect(flow.state, 'to be a', RegisterState);
|
2016-06-10 10:36:21 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|