Add tests about component

This commit is contained in:
jysperm
2015-08-16 22:04:53 +08:00
parent fa924940d8
commit 333dfe223d
15 changed files with 120 additions and 56 deletions

View File

@@ -65,8 +65,9 @@ class BillingPlan
setupDefaultComponents: (account) ->
Q.all _.values(@components).map ({type, defaults}) ->
Q.all defaults.map (defaultOptions) ->
root.components.byName(type).createComponent account, defaultOptions(account)
if defaults
Q.all defaults.map (defaultOptions) ->
root.components.byName(type).createComponent account, defaultOptions(account)
triggerTimeBilling: (account) ->
unless @billing.time
@@ -317,3 +318,5 @@ module.exports = class BillingManager
return true
else
return false
BillingManager.BillingPlan = BillingPlan

View File

@@ -111,7 +111,8 @@ Component::setStatus = (status) ->
Return {Promise}.
###
Component::destroy = ->
@provider.destroyComponent @
@populate().then =>
@provider.destroyComponent @
###
Public: Check has specified member.

View File

@@ -2,7 +2,7 @@
_ = require 'lodash'
Q = require 'q'
{Account, Ticket, Financials, CouponCode} = root
{Account, Ticket, Financials, CouponCode, Component} = root
{requireAdminAuthenticate} = require '../middleware'
module.exports = router = new Router()
@@ -17,26 +17,19 @@ router.use requireAdminAuthenticate
router.get '/dashboard', (req, res, next) ->
Q.all([
Account.find()
]).done ([accounts]) ->
Component.find()
Ticket.getTicketsGroupByStatus
opening:
limit: 10
finished:
limit: 10
closed:
limit: 10
]).done ([accounts, components, tickets]) ->
res.render 'admin',
accounts: accounts
, next
###
Router: GET /admin/tickets/list
Response HTML.
###
router.get '/tickets/list', (req, res, next) ->
Ticket.getTicketsGroupByStatus
opening:
limit: 10
finished:
limit: 10
closed:
limit: 10
.done (tickets) ->
res.render 'ticket/list', tickets
components: components
tickets: tickets
, next
###

View File

@@ -38,12 +38,12 @@ router.post '/:type', (req, res, next) ->
{account, params: {type}, body: {name, options}} = req
unless type in root.billing.availableComponents(account)
next new Error 'component_not_available'
return next new Error 'component_not_available'
root.components.byName(type).create account, root.servers.master(),
name: name
options: options
.done (component) ->
.then (component) ->
res.send component
, next

View File

@@ -3,10 +3,9 @@
describe 'manager.billing', ->
describe 'addMember and removeMember', ->
account = null
plan = null
plan = root.billing.byName 'sample'
before ->
plan = root.billing.byName 'sample'
createAccount().then (result) ->
account = result

View File

@@ -1,19 +1,18 @@
{createAccount} = helpers
{Component} = root
describe 'model.component', ->
{Component} = root
describe '.createComponent', ->
it 'should success', ->
createAccount().then (account) ->
Component.createComponent account,
name: 'linux component'
type: 'linux'
name: 'sample component'
type: 'built-in.sample'
node: 'master'
.then ->
Component.getComponents(account).then (components) ->
_.findWhere(components,
name: 'linux component'
name: 'sample component'
).should.be.exists
describe '::hasMember', ->
@@ -30,8 +29,8 @@ describe 'model.component', ->
createComponent = (options) ->
options = _.defaults {}, options,
name: 'linux component'
type: 'linux'
name: 'sample component'
type: 'built-in.sample'
node: 'master'
Q().then ->
@@ -40,4 +39,4 @@ createComponent = (options) ->
else
return createAccount()
.then (account) ->
root.Component.createComponent account, options
Component.createComponent account, options

View File

@@ -1,8 +1,7 @@
{createAccount} = helpers
{Component} = root
describe 'registry.component', ->
{Component} = root
describe 'ComponentProvider', ->
describe 'create and destroyComponent', ->
initializeCount = 0
@@ -10,8 +9,7 @@ describe 'registry.component', ->
before ->
root.components.register 'mock',
plugin:
name: 'mock'
plugin: root.plugins.byName 'built-in'
initialize: (component) ->
initializeCount++
@@ -20,14 +18,14 @@ describe 'registry.component', ->
destroyCount++
it 'should success', ->
provider = root.components.byName 'mock.mock'
provider = root.components.byName 'built-in.mock'
createAccount().then (account) ->
provider.create account, root.servers.master(),
name: 'test'
.then (component) ->
initializeCount.should.be.equal 1
component.type.should.be.equal 'mock.mock'
component.type.should.be.equal 'built-in.mock'
provider.destroyComponent(component).then ->
destroyCount.should.be.equal 1

View File

@@ -0,0 +1,7 @@
{createAdminAgent} = helpers
describe 'router.admin', ->
agent = createAdminAgent()
it 'GET /admin/dashboard', ->
agent.get '/admin/dashboard'

View File

@@ -1 +1,31 @@
{createLoggedAgent} = helpers
{Account, Component} = root
describe 'router.component', ->
agent = createLoggedAgent
baseUrl: '/components'
component_id = null
before ->
{BillingPlan} = require '../../billing-manager'
root.billing.plans['sample'].components['built-in.sample'] = {}
agent.ready.then ->
Account.search(agent.account.username).then (account) ->
root.billing.byName('sample').addMember account
it 'POST /:type', ->
agent.post '/built-in.sample',
json:
name: 'test component'
.then ({body}) ->
body.type.should.be.equal 'built-in.sample'
body.status.should.be.equal 'running'
component_id = body._id
it 'DELETE /:id', ->
agent.delete("/#{component_id}").then ->
Component.findById(component_id).then (component) ->
expect(component).to.not.exists

View File

@@ -0,0 +1 @@
describe 'router.panel', ->

View File

@@ -0,0 +1,22 @@
table.table.table-hover
thead
th 用户
th 元件模板
th 节点
th 状态
tbody
for component in components
tr
td= component.account_id
td= component.type
td= component.node
td= component.status
td
button.btn.btn-info.btn-sm.action-details(type='button')= t('common.details')
.btn-group
button(type='button', data-toggle='dropdown').btn.btn-primary.btn-sm.dropdown-toggle
| #{t('common.actions')}  
span.caret
ul.dropdown-menu
li
a.action-destroy-component(href='#') 删除元件

View File

@@ -2,7 +2,7 @@
module.exports = class Builtin extends root.Plugin
activate: ->
@injector.couponType 'cash', new CashCoupon()
@injector.couponType 'cash', cashCoupon
@injector.router('/').get '/', (req, res) ->
res.redirect '/panel/'
@@ -13,7 +13,9 @@ module.exports = class Builtin extends root.Plugin
if count == 1
account.joinGroup 'root'
class CashCoupon
@injector.component 'sample', sampleComponent
cashCoupon =
validate: (account, coupon) ->
apply_log = _.find coupon.apply_log, (log) ->
return log.account_id.equals account._id
@@ -38,3 +40,8 @@ class CashCoupon
populateCoupon: (coupon) ->
return coupon
sampleComponent =
initialize: (component) ->
destroy: (component) ->

View File

@@ -1,6 +1,6 @@
validator = require 'validator'
{requireAuthenticate} = root.middleware
{requireAuthenticate} = require root.resolve 'core/middleware'
module.exports = class Linux extends root.Plugin
activate: ->

View File

@@ -14,6 +14,8 @@ module.exports =
plugins:
'built-in':
enable: true
linux:
enable: true
server:
ssh:
@@ -37,13 +39,7 @@ module.exports =
name: 'plans.sample.name'
description: 'plans.sample.description'
available_components:
linux:
defaults: (account) ->
return {
payload:
username: account.username
}
components: {}
billing:
time:

View File

@@ -37,15 +37,15 @@ createAdmin = ->
account.joinGroup 'root'
createLoggedAgent = (options) ->
ready = null
agent = {}
agent =
account: randomAccount()
agent.ready = createAgent().post '/account/register',
json: agent.account
createAgent.methods.map (method) ->
agent[method] = (args...) ->
ready ?= createAgent().post '/account/register',
json: randomAccount()
ready.then ({body}) ->
agent.ready.then ({body}) ->
options ?= {}
options.headers ?= {}
options.headers.token = body.token
@@ -53,6 +53,13 @@ createLoggedAgent = (options) ->
return agent
createAdminAgent = (options) ->
agent = createLoggedAgent options
agent.ready.then ->
root.Account.search(agent.account.username).then (account) ->
account.joinGroup 'root'
module.exports = {
ifEnabled
unlessTravis
@@ -62,4 +69,5 @@ module.exports = {
createAccount
createAgent
createLoggedAgent
createAdminAgent
}