mirror of
https://github.com/HackPlan/RootPanel.git
synced 2026-03-26 22:16:28 +08:00
pass test: tickets.user
This commit is contained in:
@@ -7,6 +7,6 @@
|
||||
"jquery": "~2.1.2",
|
||||
"backbone": "~1.1.2",
|
||||
"moment": "~2.9.0",
|
||||
"jquery-tmpl": "*"
|
||||
"jquery-tmpl": "~1.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,11 @@ _ = require 'lodash'
|
||||
|
||||
{Account, SecurityLog, config} = root
|
||||
|
||||
builtInErrors = [
|
||||
EvalError, RangeError, ReferenceError
|
||||
SyntaxError, TypeError, URIError
|
||||
]
|
||||
|
||||
errors =
|
||||
authFailed: 403
|
||||
forbidden: 403
|
||||
@@ -104,7 +109,10 @@ exports.renderHelpers = (req, res, next) ->
|
||||
|
||||
next()
|
||||
|
||||
exports.errorhandling = (err, req, res, next) ->
|
||||
exports.errorHandling = (err, req, res, next) ->
|
||||
if err.constructor in builtInErrors
|
||||
root.log err.stack
|
||||
|
||||
res.json
|
||||
error: err.message
|
||||
|
||||
|
||||
@@ -255,7 +255,7 @@ Ticket::populateAccounts = ->
|
||||
Account.find
|
||||
_id:
|
||||
$in: [
|
||||
@account_id, @members..., _.pluck(@replies, 'account_id')...
|
||||
@account_id, @members_id..., _.pluck(@replies, 'account_id')...
|
||||
]
|
||||
|
||||
.then (accounts) =>
|
||||
@@ -265,8 +265,10 @@ Ticket::populateAccounts = ->
|
||||
@members = _.filter accounts, ({_id}) =>
|
||||
return @hasMember _id
|
||||
|
||||
@replies.each (reply) ->
|
||||
@replies.forEach (reply) ->
|
||||
reply.account = _.find accounts, ({_id}) ->
|
||||
return reply.account_id.equals _id
|
||||
|
||||
return @
|
||||
|
||||
Account = require './account'
|
||||
|
||||
@@ -174,7 +174,7 @@ module.exports = class Root extends EventEmitter
|
||||
@express.use '/account', require './router/account'
|
||||
@express.use '/components', require './router/component'
|
||||
|
||||
@express.use middleware.errorhandling
|
||||
@express.use middleware.errorHandling
|
||||
|
||||
@express.get '/', (req, res) ->
|
||||
res.redirect '/panel/'
|
||||
|
||||
@@ -28,8 +28,8 @@ describe 'account.register', ->
|
||||
body:
|
||||
account_id: /\w{24}/
|
||||
token: /\w{64}/
|
||||
.then (res) ->
|
||||
{account_id, token} = res.body
|
||||
.then ({body}) ->
|
||||
{account_id, token} = body
|
||||
|
||||
it 'POST register with existed username', ->
|
||||
agent.post '/register',
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
describe.skip 'ticket.user', ->
|
||||
agent = null
|
||||
csrf_token = null
|
||||
|
||||
ticket_id = null
|
||||
|
||||
before (done) ->
|
||||
createLoggedAgent (err, result) ->
|
||||
{agent, csrf_token} = result
|
||||
done err
|
||||
|
||||
it 'POST /', (done) ->
|
||||
agent.post '/ticket/rest/'
|
||||
.send
|
||||
csrf_token: csrf_token
|
||||
title: 'Title'
|
||||
content: '**CONTENT**'
|
||||
.expect 201
|
||||
.end (err, res) ->
|
||||
res.body.status.should.be.equal 'pending'
|
||||
ticket_id = res.body._id
|
||||
done err
|
||||
|
||||
it 'GET /', (done) ->
|
||||
agent.get '/ticket/rest/'
|
||||
.expect 200
|
||||
.end (err, res) ->
|
||||
res.body.should.be.a 'array'
|
||||
done err
|
||||
|
||||
it 'POST /:id/replies', (done) ->
|
||||
agent.post "/ticket/rest/#{ticket_id}/replies"
|
||||
.send
|
||||
csrf_token: csrf_token
|
||||
content: 'Reply'
|
||||
.expect 201
|
||||
.end (err, res) ->
|
||||
res.body._id.should.be.eixst
|
||||
res.body.content.should.be.equal 'Reply'
|
||||
done err
|
||||
|
||||
it 'GET /:id', (done) ->
|
||||
agent.get "/ticket/rest/#{ticket_id}"
|
||||
.expect 200
|
||||
.end (err, res) ->
|
||||
res.body.replies.length.should.be.equal 1
|
||||
done err
|
||||
|
||||
it 'PUT /:id/status', (done) ->
|
||||
agent.put "/ticket/rest/#{ticket_id}/status"
|
||||
.send
|
||||
csrf_token: csrf_token
|
||||
status: 'closed'
|
||||
.expect 204
|
||||
.end done
|
||||
39
core/test/tickets.user.test.coffee
Normal file
39
core/test/tickets.user.test.coffee
Normal file
@@ -0,0 +1,39 @@
|
||||
describe 'tickets.user', ->
|
||||
agent = createLoggedAgent
|
||||
baseUrl: '/tickets'
|
||||
|
||||
ticket_id = null
|
||||
|
||||
it 'POST tickets', ->
|
||||
agent.post '/',
|
||||
json:
|
||||
title: 'Title'
|
||||
content: '**CONTENT**'
|
||||
,
|
||||
body:
|
||||
status: 'pending'
|
||||
.then ({body}) ->
|
||||
ticket_id = body._id
|
||||
|
||||
it 'GET tickets', ->
|
||||
agent.get '/'
|
||||
.then ({body}) ->
|
||||
body.length.should.be.equal 1
|
||||
|
||||
it 'POST /:id/replies', ->
|
||||
agent.post "/#{ticket_id}/replies",
|
||||
json:
|
||||
content: 'Reply'
|
||||
.then ({body}) ->
|
||||
body._id.should.be.eixst
|
||||
body.content.should.be.equal 'Reply'
|
||||
|
||||
it 'GET /:id', ->
|
||||
agent.get "/#{ticket_id}"
|
||||
.then ({body}) ->
|
||||
body.replies.length.should.be.equal 1
|
||||
|
||||
it 'PUT /:id/status', ->
|
||||
agent.put "/#{ticket_id}/status",
|
||||
json:
|
||||
status: 'closed'
|
||||
@@ -1,16 +1,17 @@
|
||||
supertest = require 'supertest'
|
||||
request = require 'request'
|
||||
chai = require 'chai'
|
||||
url = require 'url'
|
||||
_ = require 'lodash'
|
||||
Q = require 'q'
|
||||
|
||||
utils = require '../core/utils'
|
||||
|
||||
expect = chai.expect
|
||||
|
||||
chai.should()
|
||||
chai.config.includeStack = true
|
||||
|
||||
utils = require '../core/utils'
|
||||
methods = ['get', 'post', 'delete', 'put', 'patch', 'head', 'options']
|
||||
|
||||
ifEnabled = (name) ->
|
||||
if name in _.keys config.plugins
|
||||
@@ -34,9 +35,7 @@ randomAccount = ->
|
||||
email: 'test' + randomLowerCase(6) + '@gmail.com'
|
||||
}
|
||||
|
||||
createAgent = (agent_options = {}) ->
|
||||
methods = ['get', 'post', 'delete', 'put', 'patch', 'head', 'options']
|
||||
|
||||
createAgent = (agent_options) ->
|
||||
if _.isNumber config.web.listen
|
||||
prefix = "http://127.0.0.1:#{config.web.listen}"
|
||||
else
|
||||
@@ -45,8 +44,8 @@ createAgent = (agent_options = {}) ->
|
||||
agent = {}
|
||||
|
||||
methods.map (method) ->
|
||||
agent[method] = (url, options = {}, asserts = {}) ->
|
||||
options = _.extend
|
||||
agent[method] = (url, options, asserts) ->
|
||||
options = _.merge
|
||||
url: url
|
||||
json: true
|
||||
method: method
|
||||
@@ -55,15 +54,13 @@ createAgent = (agent_options = {}) ->
|
||||
|
||||
if options.baseUrl
|
||||
options.baseUrl = prefix + options.baseUrl
|
||||
else
|
||||
options.baseUrl = prefix + '/'
|
||||
|
||||
Q.Promise (resolve, reject) ->
|
||||
request options, (err, res, body) ->
|
||||
if err
|
||||
reject err
|
||||
else
|
||||
resolve res
|
||||
Q.nfcall(request, options).then ([res]) ->
|
||||
return res
|
||||
.tap (res) ->
|
||||
{status, headers, body, error} = asserts
|
||||
{status, headers, body, error} = asserts ? {}
|
||||
|
||||
message = printHttpResponse res
|
||||
|
||||
@@ -78,41 +75,31 @@ createAgent = (agent_options = {}) ->
|
||||
assertObjectFields = (data, asserts) ->
|
||||
for field, pattern of asserts ? {}
|
||||
if pattern instanceof RegExp
|
||||
expect(data[field]).to.match pattern
|
||||
expect(data[field]).to.match pattern, message
|
||||
else
|
||||
expect(data[field]).to.equal pattern
|
||||
expect(data[field]).to.equal pattern, message
|
||||
|
||||
assertObjectFields res.headers, headers
|
||||
assertObjectFields res.body, body
|
||||
|
||||
return agent
|
||||
|
||||
cleanUpByAccount = ({account_id}, callback) ->
|
||||
app.models.Account.findByIdAndRemove account_id, callback
|
||||
createLoggedAgent = (options) ->
|
||||
ready = null
|
||||
agent = {}
|
||||
|
||||
createLoggedAgent = (callback) ->
|
||||
createAgent (err, {agent, csrf_token}) ->
|
||||
username = 'test' + utils.randomString(8).toLowerCase()
|
||||
email = utils.randomString(8) + '@gmail.com'
|
||||
password = utils.randomString 8
|
||||
methods.map (method) ->
|
||||
agent[method] = (args...) ->
|
||||
ready ?= createAgent().post '/account/register',
|
||||
json: randomAccount()
|
||||
|
||||
agent.post '/account/register'
|
||||
.send
|
||||
csrf_token: csrf_token
|
||||
username: username
|
||||
email: email
|
||||
password: password
|
||||
.end (err, res) ->
|
||||
after (done) ->
|
||||
cleanUpByAccount res.body.account_id, done
|
||||
ready.then ({body}) ->
|
||||
options ?= {}
|
||||
options.headers ?= {}
|
||||
options.headers.token = body.token
|
||||
createAgent(options)[method] args...
|
||||
|
||||
callback err,
|
||||
agent: agent
|
||||
username: username
|
||||
email: email
|
||||
password: password
|
||||
csrf_token: csrf_token
|
||||
account_id: res.body.account_id
|
||||
return agent
|
||||
|
||||
printHttpResponse = ({httpVersion, statusCode, statusMessage, headers, body}) ->
|
||||
message = """
|
||||
@@ -126,13 +113,14 @@ printHttpResponse = ({httpVersion, statusCode, statusMessage, headers, body}) ->
|
||||
if headers['content-type']?.match /text\/html/
|
||||
body = body.replace / /g, ' '
|
||||
body = body.replace /<br>/g, '\n'
|
||||
else if headers['content-type']?.match /application\/json/
|
||||
body = JSON.stringify body, null, ' '
|
||||
|
||||
message += "\n#{body}"
|
||||
|
||||
return message
|
||||
|
||||
_.extend global, {
|
||||
supertest
|
||||
utils
|
||||
|
||||
ifEnabled
|
||||
@@ -140,6 +128,5 @@ _.extend global, {
|
||||
randomAccount
|
||||
|
||||
createAgent
|
||||
cleanUpByAccount
|
||||
createLoggedAgent
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user