PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/test/authorizer.js

https://gitlab.com/harshit-bangar/mosca
JavaScript | 297 lines | 252 code | 45 blank | 0 comment | 0 complexity | 114484d6fe383b0b79950bb05ea6344c MD5 | raw file
  1. "use strict";
  2. var hasher = require("pbkdf2-password")();
  3. var async = require("async");
  4. describe("mosca.Authorizer", function() {
  5. var authorizer, instance, client;
  6. beforeEach(function() {
  7. authorizer = new mosca.Authorizer();
  8. client = {};
  9. });
  10. describe("authenticate", function() {
  11. beforeEach(function() {
  12. instance = authorizer.authenticate;
  13. });
  14. it("it should not authenticate an unknown user", function(done) {
  15. instance(client, "user", "pass", function(err, success) {
  16. expect(success).to.be.false;
  17. done();
  18. });
  19. });
  20. it("it should authenticate a known user", function(done) {
  21. authorizer.addUser("user", "pass", function() {
  22. instance(client, "user", "pass", function(err, success) {
  23. expect(success).to.be.true;
  24. done();
  25. });
  26. });
  27. });
  28. it("it should not authenticate a user with the wrong password", function(done) {
  29. authorizer.addUser("user", "pass", function() {
  30. instance(client, "user", "wrongpass", function(err, success) {
  31. expect(success).to.be.false;
  32. done();
  33. });
  34. });
  35. });
  36. it("it should not authenticate a user without a password", function(done) {
  37. authorizer.addUser("user", "pass", function() {
  38. instance(client, "user", null, function(err, success) {
  39. expect(success).to.be.false;
  40. done();
  41. });
  42. });
  43. });
  44. it("it should not authenticate a user without a username", function(done) {
  45. authorizer.addUser("user", "pass", function() {
  46. instance(client, null, "pass", function(err, success) {
  47. expect(success).to.be.false;
  48. done();
  49. });
  50. });
  51. });
  52. it("it should authenticate a user known user", function(done) {
  53. authorizer.addUser("matteo", "collina", function() {
  54. instance(client, "matteo", "collina", function(err, success) {
  55. expect(success).to.be.true;
  56. done();
  57. });
  58. });
  59. });
  60. it("it should not authenticate a removed user", function(done) {
  61. async.waterfall([
  62. authorizer.addUser.bind(authorizer, "matteo", "collina"),
  63. authorizer.rmUser.bind(authorizer, "matteo"),
  64. instance.bind(null, client, "matteo", "collina")
  65. ], function(err, success) {
  66. expect(success).to.be.false;
  67. done();
  68. });
  69. });
  70. it("it should add the username to the client", function(done) {
  71. authorizer.addUser("user", "pass", function() {
  72. instance(client, "user", "pass", function(err, success) {
  73. expect(client).to.have.property("user", "user");
  74. done();
  75. });
  76. });
  77. });
  78. });
  79. describe("users", function() {
  80. beforeEach(function() {
  81. instance = authorizer;
  82. });
  83. it("should memorize a user", function(done) {
  84. instance.addUser("matteo", "collina", function() {
  85. expect(instance.users.matteo).to.exist;
  86. done();
  87. });
  88. });
  89. it("should memorize a user has salt/hash combination", function(done) {
  90. instance.addUser("matteo", "collina", function() {
  91. expect(instance.users.matteo.salt).to.exist;
  92. expect(instance.users.matteo.hash).to.exist;
  93. done();
  94. });
  95. });
  96. it("should be a real hash", function(done) {
  97. instance.addUser("matteo", "collina", function() {
  98. hasher({
  99. password: "collina",
  100. salt: instance.users.matteo.salt
  101. },
  102. function(err, pass, salt, hash) {
  103. expect(hash).to.eql(instance.users.matteo.hash);
  104. done();
  105. });
  106. });
  107. });
  108. });
  109. it("should support passing users as a parameter", function() {
  110. var users = {};
  111. instance = new mosca.Authorizer(users);
  112. expect(instance.users).to.equal(users);
  113. });
  114. describe("authorizePublish", function() {
  115. beforeEach(function(done) {
  116. client.user = "user";
  117. instance = authorizer.authorizePublish;
  118. authorizer.addUser("user", "pass", function() {
  119. done();
  120. });
  121. });
  122. it("it should authorize a publish based on the topic", function(done) {
  123. instance(client, "topic", "payload", function(err, success) {
  124. expect(success).to.be.true;
  125. done();
  126. });
  127. });
  128. it("it should authorize a publish based on a long topic", function(done) {
  129. instance(client, "/long/topic", "payload", function(err, success) {
  130. expect(success).to.be.true;
  131. done();
  132. });
  133. });
  134. it("it should not authorize a publish based on the topic", function(done) {
  135. authorizer.addUser("user", "pass", "/topic", function() {
  136. instance(client, "other", "payload", function(err, success) {
  137. expect(success).to.be.false;
  138. done();
  139. });
  140. });
  141. });
  142. it("should default the authorizePublish param to **", function(done) {
  143. authorizer.addUser("user", "pass", null, function() {
  144. instance(client, "other", "payload", function(err, success) {
  145. expect(success).to.be.true;
  146. done();
  147. });
  148. });
  149. });
  150. it("it should authorize a publish based on a pattern", function(done) {
  151. authorizer.addUser("user", "pass", "/topic/*", function() {
  152. instance(client, "/topic/other", "payload", function(err, success) {
  153. expect(success).to.be.true;
  154. done();
  155. });
  156. });
  157. });
  158. it("it should not authorize a publish based on a pattern", function(done) {
  159. authorizer.addUser("user", "pass", "/topic/*", function() {
  160. instance(client, "/topic/other/buu", "payload", function(err, success) {
  161. expect(success).to.be.false;
  162. done();
  163. });
  164. });
  165. });
  166. it("it should authorize a publish based on a unlimited pattern", function(done) {
  167. authorizer.addUser("user", "pass", "/topic/**", function() {
  168. instance(client, "/topic/other/buu", "payload", function(err, success) {
  169. expect(success).to.be.true;
  170. done();
  171. });
  172. });
  173. });
  174. it("it should authorize a publish based on a recursive pattern", function(done) {
  175. authorizer.addUser("user", "pass", "/topic/**/buu", function() {
  176. instance(client, "/topic/other/long/buu", "payload", function(err, success) {
  177. expect(success).to.be.true;
  178. done();
  179. });
  180. });
  181. });
  182. });
  183. describe("authorizeSubscribe", function() {
  184. beforeEach(function(done) {
  185. client.user = "user";
  186. instance = authorizer.authorizeSubscribe;
  187. authorizer.addUser("user", "pass", function() {
  188. done();
  189. });
  190. });
  191. it("it should authorize a subscribe based on the topic", function(done) {
  192. instance(client, "topic", function(err, success) {
  193. expect(success).to.be.true;
  194. done();
  195. });
  196. });
  197. it("it should authorize a publish based on a long topic", function(done) {
  198. instance(client, "/long/topic", function(err, success) {
  199. expect(success).to.be.true;
  200. done();
  201. });
  202. });
  203. it("should default the authorizeSubscribe param to **", function(done) {
  204. authorizer.addUser("user", "pass", null, null, function() {
  205. instance(client, "other", function(err, success) {
  206. expect(success).to.be.true;
  207. done();
  208. });
  209. });
  210. });
  211. it("it should not authorize a publish based on the topic", function(done) {
  212. authorizer.addUser("user", "pass", "**", "/topic", function() {
  213. instance(client, "other", function(err, success) {
  214. expect(success).to.be.false;
  215. done();
  216. });
  217. });
  218. });
  219. it("it should authorize a publish based on a pattern", function(done) {
  220. authorizer.addUser("user", "pass", "**", "/topic/*", function() {
  221. instance(client, "/topic/other", function(err, success) {
  222. expect(success).to.be.true;
  223. done();
  224. });
  225. });
  226. });
  227. it("it should not authorize a publish based on a pattern", function(done) {
  228. authorizer.addUser("user", "pass", "**", "/topic/*", function() {
  229. instance(client, "/topic/other/buu", function(err, success) {
  230. expect(success).to.be.false;
  231. done();
  232. });
  233. });
  234. });
  235. it("it should authorize a publish based on a unlimited pattern", function(done) {
  236. authorizer.addUser("user", "pass", "**", "/topic/**", function() {
  237. instance(client, "/topic/other/buu", function(err, success) {
  238. expect(success).to.be.true;
  239. done();
  240. });
  241. });
  242. });
  243. it("it should authorize a publish based on a recursive pattern", function(done) {
  244. authorizer.addUser("user", "pass", "**", "/topic/**/buu", function() {
  245. instance(client, "/topic/other/long/buu", function(err, success) {
  246. expect(success).to.be.true;
  247. done();
  248. });
  249. });
  250. });
  251. });
  252. });