auth.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import assert from 'assert';
  2. import reducer, { SIGN_IN, SIGN_OUT, signIn, signOut } from 'misago/reducers/auth';
  3. import { Auth } from 'misago/services/auth';
  4. let auth = null;
  5. describe("Auth", function() {
  6. it("synces authenticated session", function(done) {
  7. let store = {
  8. getState: function() {
  9. return {
  10. auth: {
  11. isAuthenticated: true,
  12. user: {
  13. username: 'BobBoberson'
  14. }
  15. }
  16. };
  17. }
  18. };
  19. let local = {
  20. set: function(name, value) {
  21. assert.equal(name, 'auth', "synced session key is valid");
  22. assert.deepEqual(value, {
  23. isAuthenticated: true,
  24. username: 'BobBoberson'
  25. }, "new session state is valid");
  26. done();
  27. },
  28. watch: function() {
  29. /* noop */
  30. }
  31. };
  32. auth = new Auth();
  33. auth.init(store, local);
  34. });
  35. it("synces anonymous session", function(done) {
  36. let store = {
  37. getState: function() {
  38. return {
  39. auth: {
  40. isAuthenticated: false
  41. }
  42. };
  43. }
  44. };
  45. let local = {
  46. set: function(name, value) {
  47. assert.equal(name, 'auth', "synced session key is valid");
  48. assert.deepEqual(value, {
  49. isAuthenticated: false
  50. }, "new session state is valid");
  51. done();
  52. },
  53. watch: function() {
  54. /* noop */
  55. }
  56. };
  57. auth = new Auth();
  58. auth.init(store, local);
  59. });
  60. it("watches session sign in", function(done) {
  61. let store = {
  62. getState: function() {
  63. return {
  64. auth: {
  65. isAuthenticated: false
  66. }
  67. };
  68. },
  69. dispatch(action) {
  70. assert.deepEqual(action, {
  71. type: SIGN_IN,
  72. user: {
  73. username: 'BobBoberson'
  74. }
  75. }, "action was dispatched");
  76. done();
  77. }
  78. };
  79. let local = {
  80. set: function() {
  81. /* noop */
  82. },
  83. watch: function(name, callable) {
  84. assert.equal(name, 'auth', "watched session key is valid");
  85. assert.ok(callable, "callback is provided");
  86. callable({
  87. isAuthenticated: true,
  88. username: 'BobBoberson'
  89. });
  90. }
  91. };
  92. auth = new Auth();
  93. auth.init(store, local);
  94. });
  95. it("watches session sign out", function(done) {
  96. let store = {
  97. getState: function() {
  98. return {
  99. auth: {
  100. isAuthenticated: false
  101. }
  102. };
  103. },
  104. dispatch(action) {
  105. assert.deepEqual(action, {
  106. type: SIGN_OUT,
  107. soft: false
  108. }, "action was dispatched");
  109. done();
  110. }
  111. };
  112. let local = {
  113. set: function() {
  114. /* noop */
  115. },
  116. watch: function(name, callable) {
  117. assert.equal(name, 'auth', "watched session key is valid");
  118. assert.ok(callable, "callback is provided");
  119. callable({
  120. isAuthenticated: false
  121. });
  122. }
  123. };
  124. auth = new Auth();
  125. auth.init(store, local);
  126. });
  127. });
  128. describe("Auth Reducer", function() {
  129. it("signs user in", function() {
  130. let state = {
  131. signedIn: false
  132. };
  133. assert.deepEqual(reducer(state, signIn({username: 'Weebl'})), {
  134. signedIn: {
  135. username: 'Weebl'
  136. }
  137. }, "reducer changed store state for sign in");
  138. });
  139. it("signs user out", function() {
  140. let state = {
  141. isAuthenticated: true,
  142. isAnonymous: true,
  143. signedIn: false,
  144. signedOut: true
  145. };
  146. assert.deepEqual(reducer(state, signOut()), {
  147. isAuthenticated: false,
  148. isAnonymous: true,
  149. signedIn: false,
  150. signedOut: true
  151. }, "reducer changed store state for sign out");
  152. });
  153. it("soflty signs user out", function() {
  154. let state = {
  155. isAuthenticated: true,
  156. isAnonymous: true,
  157. signedIn: false,
  158. signedOut: true
  159. };
  160. assert.deepEqual(reducer(state, signOut(true)), {
  161. isAuthenticated: false,
  162. isAnonymous: true,
  163. signedIn: false,
  164. signedOut: false
  165. }, "reducer changed store state for soft sign out");
  166. });
  167. });