Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
162
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. password: 'pw'
  39. body = JSON.stringify(data)
  40. request {url: url, method: 'POST',json: data, headers: headers }, (err, response, body)->
  41.  
  42. expect(response.statusCode).toEqual(200)
  43. done()
  44.  
  45. exports.init = function (account) {
  46. // set account object
  47. }
  48.  
  49. describe('some tests', function () {
  50.  
  51. var account, response, testObject;
  52.  
  53. beforeEach(function () {
  54.  
  55. account = {
  56. register: function () { }
  57. };
  58.  
  59. response = {
  60. send: function () { }
  61. };
  62.  
  63. testObject = require('./auth');
  64. testObject.init(account);
  65. });
  66.  
  67. it('should test something', function () {
  68.  
  69. var req = { body: { email: ..., password: .... } }, // the request to test
  70. resMock = sinon.mock(response),
  71. registerStub = sinon.stub(account, 'register');
  72.  
  73. // the request expectations
  74. resMock.expect('send').once().withArgs(200);
  75.  
  76. // the stub for the register method to have some process
  77. registerStub.once().withArgs('someargs');
  78.  
  79. testObject.auth(req. response);
  80.  
  81. resMock.verify();
  82.  
  83. });
  84.  
  85. });
  86.  
  87. describe 'register', ->
  88. account = response = routes_auth = null
  89.  
  90. beforeEach ->
  91. account =
  92. register: (email, pw, callback)->
  93. if email is '[email protected]'
  94. callback(null, 1)
  95. else
  96. err = 'error'
  97. callback(err, 0)
  98.  
  99. response =
  100. send: -> {}
  101.  
  102. routes_auth = require('../routes/auth').init(account)
  103.  
  104.  
  105. it 'should register a user with email and pw', (done)->
  106. req =
  107. body:
  108. password: 'pw'
  109.  
  110. resMock = sinon.mock(response)
  111. resMock.expects('send').once().withArgs(200)
  112. routes_auth.post_register(req, response)
  113. resMock.verify()
  114. done()
  115.  
  116.  
  117.  
  118. it 'should not register a user without email', ()->
  119. req =
  120. body:
  121. password: 'pw'
  122.  
  123. resMock = sinon.mock(response)
  124. resMock.expects('send').once().withArgs(400)
  125. routes_auth.post_register(req, response)
  126. resMock.verify()
  127.  
  128. exports.init = (account)->
  129. get_available: (req, res)->
  130. email = req.param.email
  131. if not email? or email.length < 1
  132. res.send 400
  133. return
  134. account.available email, (err, doc)->
  135. console.log 'get_available', err, doc
  136. if err then res.send 401
  137. else res.send 200
  138.  
  139.  
  140. post_register: (req, res)->
  141. email = req.body.email
  142. password = req.body.password
  143. if email and password
  144. account.register email, password, (err, doc)->
  145. if err then res.send 401
  146. else res.send 200
  147. else
  148. res.send 400
  149.  
  150. describe('#Store() ', function () {
  151. it('will delegate the store to the CacheItem and CacheKey', function () {
  152. var actualCacheKey, actualConnMgr, actualConfig, actualLogger, actualRequest;
  153. var actualKeyRequest, actualKeyConfig;
  154.  
  155. gently.expect(
  156. CacheKey, 'CreateInstance', function (apiRequest, config) {
  157. actualKeyRequest = apiRequest;
  158. actualKeyConfig = config;
  159.  
  160. return mockCacheKey;
  161. });
  162.  
  163. gently.expect(
  164. CacheItem, 'CreateInstance', function (cacheKey, connectionManager, config, logger, apiRequest) {
  165. actualCacheKey = cacheKey;
  166. actualConnMgr = connectionManager;
  167. actualConfig = config;
  168. actualLogger = logger;
  169. actualRequest = apiRequest;
  170.  
  171. return mockCacheItem;
  172. });
  173.  
  174. var actualApiRequest, actualCallback;
  175. gently.expect(mockCacheItem, 'Store', function (request, callback) {
  176. actualApiRequest = request;
  177. actualCallback = callback;
  178. });
  179.  
  180. var callback = function () {};
  181. var apiResponse = {'item': 'this is a sample response from SAS'};
  182. Cache.GetInstance(connMgr, config, logger).Store(apiRequest, apiResponse, callback);
  183.  
  184. mockCacheKey.should.be.equal(actualCacheKey, 'The cachkeKey to CacheItem.CreateIntsance() did not match');
  185. connMgr.should.be.equal(
  186. actualConnMgr, 'The connection manager to CacheItem.CreateInstance() did not match');
  187. config.should.be.equal(actualConfig, 'The config to CacheItem.CreateInstance() did not match');
  188. logger.should.be.equal(actualLogger, 'The logger to CacheItem.Createinstance did not match');
  189. apiRequest.should.be.equal(actualRequest, 'The request to CacheItem.Createinstance() did not match');
  190.  
  191. apiRequest.should.be.equal(actualKeyRequest, 'The request to CacheKey.CreateInstance() did not match');
  192. config.should.be.equal(actualKeyConfig, 'The config to CacheKey.CreateInstance() did not match');
  193.  
  194. callback.should.be.equal(actualCallback, 'The callback passed to CacheItem.Store() did not match');
  195. apiResponse.should.be.equal(actualApiRequest, 'The apiRequest passed to CacheItem.Store() did not match');
  196. });
  197. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement