|
1 | 1 | import {expectSaga} from 'redux-saga-test-plan' |
2 | 2 | import * as matchers from 'redux-saga-test-plan/matchers' |
3 | | -import {login, rest} from 'tocco-app-extensions' |
| 3 | +import {select} from 'redux-saga/effects' |
| 4 | +import {login, notification, rest} from 'tocco-app-extensions' |
4 | 5 |
|
| 6 | +import * as actions from './actions' |
5 | 7 | import * as sagas from './sagas' |
6 | 8 |
|
7 | 9 | describe('admin', () => { |
@@ -59,24 +61,93 @@ describe('admin', () => { |
59 | 61 | }) |
60 | 62 | }) |
61 | 63 | describe('sessionHeartbeat', () => { |
62 | | - test('should set flags and call itself', () => { |
| 64 | + test('should set flags if user is logged in successfully', () => { |
63 | 65 | const sessionResponse = { |
64 | 66 | success: true, |
65 | 67 | adminAllowed: true |
66 | 68 | } |
67 | 69 |
|
68 | 70 | return expectSaga(sagas.sessionHeartbeat) |
69 | 71 | .provide([ |
| 72 | + [select(sagas.loginSelector), {loggedIn: false}], |
70 | 73 | [matchers.call.fn(login.doSessionRequest), sessionResponse], |
71 | 74 | [matchers.call.fn(sagas.sessionHeartbeat)], |
72 | 75 | [matchers.call.fn(sagas.delayByTimeout)] |
73 | 76 | ]) |
74 | 77 | .put(login.setLoggedIn(true)) |
75 | 78 | .put(login.setAdminAllowed(true)) |
| 79 | + .put(actions.setInvalidSession(false)) |
76 | 80 | .call(sagas.sessionHeartbeat) |
77 | 81 | .call(sagas.delayByTimeout, sagas.HEARTBEAT_INTERVAL_IN_MS) |
78 | 82 | .run() |
79 | 83 | }) |
| 84 | + |
| 85 | + test('should set flags if user is already logged in', () => { |
| 86 | + const sessionResponse = { |
| 87 | + success: true, |
| 88 | + adminAllowed: true |
| 89 | + } |
| 90 | + |
| 91 | + return expectSaga(sagas.sessionHeartbeat) |
| 92 | + .provide([ |
| 93 | + [select(sagas.loginSelector), {loggedIn: true}], |
| 94 | + [matchers.call.fn(login.doSessionRequest), sessionResponse], |
| 95 | + [matchers.call.fn(sagas.sessionHeartbeat)], |
| 96 | + [matchers.call.fn(sagas.delayByTimeout)] |
| 97 | + ]) |
| 98 | + .put(login.setLoggedIn(true)) |
| 99 | + .put(login.setAdminAllowed(true)) |
| 100 | + .put(actions.setInvalidSession(false)) |
| 101 | + .call(sagas.sessionHeartbeat) |
| 102 | + .call(sagas.delayByTimeout, sagas.HEARTBEAT_INTERVAL_IN_MS) |
| 103 | + .run() |
| 104 | + }) |
| 105 | + |
| 106 | + test('should set invalid session if user is no longer logged in', () => { |
| 107 | + const sessionResponse = { |
| 108 | + success: false |
| 109 | + } |
| 110 | + |
| 111 | + return expectSaga(sagas.sessionHeartbeat) |
| 112 | + .provide([ |
| 113 | + [select(sagas.loginSelector), {loggedIn: true}], |
| 114 | + [matchers.call.fn(login.doSessionRequest), sessionResponse], |
| 115 | + [matchers.call.fn(sagas.sessionHeartbeat)], |
| 116 | + [matchers.call.fn(sagas.delayByTimeout)] |
| 117 | + ]) |
| 118 | + .not.put(login.setLoggedIn(false)) |
| 119 | + .put(actions.setInvalidSession(true)) |
| 120 | + .call(sagas.sessionHeartbeat) |
| 121 | + .call(sagas.delayByTimeout, sagas.HEARTBEAT_INTERVAL_IN_MS) |
| 122 | + .run() |
| 123 | + }) |
| 124 | + }) |
| 125 | + |
| 126 | + describe('loginSuccessful', () => { |
| 127 | + test('login and set admin allowed to undefined', () => { |
| 128 | + return expectSaga(sagas.loginSuccessful) |
| 129 | + .provide([ |
| 130 | + [select(sagas.sessionSelector), {invalidSession: false}], |
| 131 | + [matchers.call.fn(sagas.doSessionRequest)] |
| 132 | + ]) |
| 133 | + .put(login.setAdminAllowed(undefined)) |
| 134 | + .put(login.setLoggedIn(true)) |
| 135 | + .put(notification.connectSocket()) |
| 136 | + .call(sagas.doSessionRequest) |
| 137 | + .run() |
| 138 | + }) |
| 139 | + |
| 140 | + test('relogin after invalid session', () => { |
| 141 | + return expectSaga(sagas.loginSuccessful) |
| 142 | + .provide([ |
| 143 | + [select(sagas.sessionSelector), {invalidSession: true}], |
| 144 | + [matchers.call.fn(sagas.doSessionRequest)] |
| 145 | + ]) |
| 146 | + .put(login.setLoggedIn(true)) |
| 147 | + .put(notification.connectSocket()) |
| 148 | + .call(sagas.doSessionRequest) |
| 149 | + .run() |
| 150 | + }) |
80 | 151 | }) |
81 | 152 | }) |
82 | 153 | }) |
|
0 commit comments