auth.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. }, "action was dispatched");
  108. done();
  109. }
  110. };
  111. let local = {
  112. set: function() {
  113. /* noop */
  114. },
  115. watch: function(name, callable) {
  116. assert.equal(name, 'auth', "watched session key is valid");
  117. assert.ok(callable, "callback is provided");
  118. callable({
  119. isAuthenticated: false
  120. });
  121. }
  122. };
  123. auth = new Auth();
  124. auth.init(store, local);
  125. });
  126. });
  127. describe("Auth Reducer", function() {
  128. it("signs user in", function() {
  129. let state = {
  130. signedIn: false
  131. };
  132. assert.deepEqual(reducer(state, signIn({username: 'Weebl'})), {
  133. signedIn: {
  134. username: 'Weebl'
  135. }
  136. }, "reducer changed store state for sign in");
  137. });
  138. it("signs user out", function() {
  139. let state = {
  140. isAuthenticated: true,
  141. isAnonymous: true,
  142. signedIn: false,
  143. signedOut: true
  144. };
  145. assert.deepEqual(reducer(state, signOut()), {
  146. isAuthenticated: false,
  147. isAnonymous: true,
  148. signedIn: false,
  149. signedOut: true
  150. }, "reducer changed store state for sign out");
  151. });
  152. });