Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. # models/account.coffee
  2. register = (email, password)->
  3. sha_sum.update(password)
  4. pw = sha_sum.digest('hex')
  5. user =
  6. email: email
  7. password: sha_sum.digest('hex')
  8.  
  9. users_db.save user, (err, doc)->
  10. register_callback(err)
  11.  
  12. account_module =
  13. register: register
  14.  
  15. module.exports = account_module
  16.  
  17. # routes/auth.coffee
  18. account = require '../models/account'
  19.  
  20. exports.auth =
  21. post_signup: (req, res)->
  22. email = req.body.email
  23. password = req.body.password
  24. if email and password
  25. account.register(email, password)
  26. res.send 200
  27. else
  28. res.send 400
  29.  
  30. request = require 'request'
  31. it 'should signup a user with username and password', (done)->
  32.  
  33. spyOn(account, 'register') # this does not work, account.register still called
  34. url = root + '/signup'
  35. headers =
  36. "Content-Type": "application/json"
  37. data =
  38. email: 'user@email.com'
  39. password: 'pw'
  40. body = JSON.stringify(data)
  41. request {url: url, method: 'POST',json: data, headers: headers }, (err, response, body)->
  42.  
  43. expect(response.statusCode).toEqual(200)
  44. done()
  45.  
  46. exports.init = function (account) {
  47. // set account object
  48. }
  49.  
  50. describe('some tests', function () {
  51.  
  52. var account, response, testObject;
  53.  
  54. beforeEach(function () {
  55.  
  56. account = {
  57. register: function () { }
  58. };
  59.  
  60. response = {
  61. send: function () { }
  62. };
  63.  
  64. testObject = require('./auth');
  65. testObject.init(account);
  66. });
  67.  
  68. it('should test something', function () {
  69.  
  70. var req = { body: { email: ..., password: .... } }, // the request to test
  71. resMock = sinon.mock(response),
  72. registerStub = sinon.stub(account, 'register');
  73.  
  74. // the request expectations
  75. resMock.expect('send').once().withArgs(200);
  76.  
  77. // the stub for the register method to have some process
  78. registerStub.once().withArgs('someargs');
  79.  
  80. testObject.auth(req. response);
  81.  
  82. resMock.verify();
  83.  
  84. });
  85.  
  86. });
  87.  
  88. describe 'register', ->
  89. account = response = routes_auth = null
  90.  
  91. beforeEach ->
  92. account =
  93. register: (email, pw, callback)->
  94. if email is 'valid@email.com'
  95. callback(null, 1)
  96. else
  97. err = 'error'
  98. callback(err, 0)
  99.  
  100. response =
  101. send: -> {}
  102.  
  103. routes_auth = require('../routes/auth').init(account)
  104.  
  105.  
  106. it 'should register a user with email and pw', (done)->
  107. req =
  108. body:
  109. email: 'valid@email.com'
  110. password: 'pw'
  111.  
  112. resMock = sinon.mock(response)
  113. resMock.expects('send').once().withArgs(200)
  114. routes_auth.post_register(req, response)
  115. resMock.verify()
  116. done()
  117.  
  118.  
  119.  
  120. it 'should not register a user without email', ()->
  121. req =
  122. body:
  123. password: 'pw'
  124.  
  125. resMock = sinon.mock(response)
  126. resMock.expects('send').once().withArgs(400)
  127. routes_auth.post_register(req, response)
  128. resMock.verify()
  129.  
  130. exports.init = (account)->
  131. get_available: (req, res)->
  132. email = req.param.email
  133. if not email? or email.length < 1
  134. res.send 400
  135. return
  136. account.available email, (err, doc)->
  137. console.log 'get_available', err, doc
  138. if err then res.send 401
  139. else res.send 200
  140.  
  141.  
  142. post_register: (req, res)->
  143. email = req.body.email
  144. password = req.body.password
  145. if email and password
  146. account.register email, password, (err, doc)->
  147. if err then res.send 401
  148. else res.send 200
  149. else
  150. res.send 400
  151.  
  152. describe('#Store() ', function () {
  153. it('will delegate the store to the CacheItem and CacheKey', function () {
  154. var actualCacheKey, actualConnMgr, actualConfig, actualLogger, actualRequest;
  155. var actualKeyRequest, actualKeyConfig;
  156.  
  157. gently.expect(
  158. CacheKey, 'CreateInstance', function (apiRequest, config) {
  159. actualKeyRequest = apiRequest;
  160. actualKeyConfig = config;
  161.  
  162. return mockCacheKey;
  163. });
  164.  
  165. gently.expect(
  166. CacheItem, 'CreateInstance', function (cacheKey, connectionManager, config, logger, apiRequest) {
  167. actualCacheKey = cacheKey;
  168. actualConnMgr = connectionManager;
  169. actualConfig = config;
  170. actualLogger = logger;
  171. actualRequest = apiRequest;
  172.  
  173. return mockCacheItem;
  174. });
  175.  
  176. var actualApiRequest, actualCallback;
  177. gently.expect(mockCacheItem, 'Store', function (request, callback) {
  178. actualApiRequest = request;
  179. actualCallback = callback;
  180. });
  181.  
  182. var callback = function () {};
  183. var apiResponse = {'item': 'this is a sample response from SAS'};
  184. Cache.GetInstance(connMgr, config, logger).Store(apiRequest, apiResponse, callback);
  185.  
  186. mockCacheKey.should.be.equal(actualCacheKey, 'The cachkeKey to CacheItem.CreateIntsance() did not match');
  187. connMgr.should.be.equal(
  188. actualConnMgr, 'The connection manager to CacheItem.CreateInstance() did not match');
  189. config.should.be.equal(actualConfig, 'The config to CacheItem.CreateInstance() did not match');
  190. logger.should.be.equal(actualLogger, 'The logger to CacheItem.Createinstance did not match');
  191. apiRequest.should.be.equal(actualRequest, 'The request to CacheItem.Createinstance() did not match');
  192.  
  193. apiRequest.should.be.equal(actualKeyRequest, 'The request to CacheKey.CreateInstance() did not match');
  194. config.should.be.equal(actualKeyConfig, 'The config to CacheKey.CreateInstance() did not match');
  195.  
  196. callback.should.be.equal(actualCallback, 'The callback passed to CacheItem.Store() did not match');
  197. apiResponse.should.be.equal(actualApiRequest, 'The apiRequest passed to CacheItem.Store() did not match');
  198. });
  199. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement