forked from cpatni/aasm-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_machine.spec.coffee
87 lines (66 loc) · 2.35 KB
/
auth_machine.spec.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
AASM = require '../lib/aasm'
class AuthMachine
AASM.include(this)
#properties activationCode, activatedAt, deletedAt
@aasmInitialState 'pending'
@aasmState 'passive'
@aasmState 'pending', enter: 'makeActivationCode'
@aasmState 'active', enter: 'doActivate'
@aasmState 'suspended'
@aasmState 'deleted', enter: 'doDelete', exit: 'doUndelete'
@aasmEvent 'register', ->
@transitions from: 'passive', to: 'pending', guard: () -> @canRegister()
@aasmEvent 'activate', ->
@transitions from: 'pending', to: 'active'
@aasmEvent 'suspend', ->
@transitions from: ['passive', 'pending', 'active'], to: 'suspended'
@aasmEvent 'delete', ->
@transitions from: ['passive', 'pending', 'active', 'suspended'], to: 'deleted'
@aasmEvent 'unsuspend', ->
@transitions from: 'suspended', to: 'active', guard: () -> @hasActivated()
@transitions from: 'suspended', to: 'pending', guard: () -> @hasActivationCode()
@transitions from: 'suspended', to: 'passive'
constructor: () ->
@aasmEnterInitialState()
makeActivationCode: () ->
@activationCode = 'moo'
doActivate: () ->
@activatedAt = Date.now()
@activationCode = null
doDelete: () ->
@deletedAt = Date.now()
doUndelete: () ->
@deletedAt = false
canRegister: ()->
true
hasActivated: () ->
@activatedAt?
hasActivationCode: () ->
@activationCode?
describe [AuthMachine, 'authentication state machine'], ->
describe "initialization", ->
it 'should be in pending state', ->
auth = new AuthMachine()
expect(auth.aasmCurrentState()).toEqual('pending')
it 'should have an activation code', ->
auth = new AuthMachine()
expect(auth.hasActivationCode()).toBeTruthy()
expect(auth.activationCode).toNotEqual(null)
describe 'when being unsuspended', ->
it 'should be active if previously acticated', ->
auth = new AuthMachine()
auth.activate()
auth.suspend()
auth.unsuspend()
expect(auth.aasmCurrentState()).toEqual('active')
it 'should be pending if not previously activated, but an activation code is present', ->
auth = new AuthMachine()
auth.suspend()
auth.unsuspend()
expect(auth.aasmCurrentState()).toEqual('pending')
it 'should be passive if not previously activated and there is no activation code', ->
auth = new AuthMachine()
auth.activationCode = null
auth.suspend()
auth.unsuspend()
expect(auth.aasmCurrentState()).toEqual('passive')