Skip to content

Commit 75ea2e3

Browse files
committed
Added more tests for user.
1 parent 87190c4 commit 75ea2e3

4 files changed

Lines changed: 144 additions & 44 deletions

File tree

‎models/model.coffee‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
clone = require 'clone'
12
StoredObject = require '../data/storedObject'
23

34
class Model extends StoredObject
5+
constructor: (attributes) ->
6+
@attributes = clone(attributes) || {}
7+
48
save: (collection, callback) ->
59
super @collection, @toJSON(), callback
610

711
toJSON: () ->
8-
{}
12+
clone(@attributes)
913

1014
module.exports = Model

‎models/user.coffee‎

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
Model = require './model'
22

3-
class User extends Model
4-
constructor: (attributes = {}) ->
5-
@name = attributes.name || undefined
6-
@userSince = attributes.userSince || undefined
7-
@slugs = attributes.slugs || []
8-
@handles = attributes.handles || []
9-
@credentials = attributes.credentials || []
10-
@_id = attributes._id
11-
12-
setCredential: (provider, id, callback) =>
13-
@credentials.push
14-
provider: provider,
15-
id: id
16-
17-
@save('user', callback)
3+
defaults =
4+
slugs: []
5+
credentials: []
6+
handles: []
187

19-
toJSON: () =>
20-
name: @name
21-
slugs: @slugs
22-
handles: @handles
23-
credentials: @credentials
24-
_id: @_id
8+
class User extends Model
9+
constructor: (attributes = defaults) ->
10+
super attributes
11+
12+
setCredential: (provider, id) ->
13+
@attributes.credentials or= []
2514

15+
existingAttributes = @attributes.credentials.filter (c) ->
16+
c.provider.toLowerCase().trim() == provider.toLowerCase().trim()
17+
18+
if existingAttributes.length > 0
19+
existingAttributes[0].id = id
20+
else
21+
@attributes.credentials.push
22+
provider: provider
23+
id: id
24+
2625
module.exports = User

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"dependencies": {
55
"express": "~v2.5.6",
66
"coffee-script": "~v1.2.0",
7+
"clone", "v0.0.4",
78
"jade": "~v0.20.0",
89
"stylus": "~v0.22.5",
910
"passport": "~v0.1.6",

‎test/models/user_test.coffee‎

Lines changed: 118 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,138 @@ User = require '../../models/user'
44

55
expectedAttributes =
66
name: 'Abraham Lincoln'
7-
userSince: new Date()
7+
userSince: Date.parse('April 14, 1865')
88
slugs: ['abe', 'penniesrock']
99
handles: [{type: 'twitter', handle: 'abelincoln'}]
1010
credentials: [{provider: 'twitter', id: 1234 }]
1111
_id: 1234
1212

1313
vows.describe('User (models/user)').addBatch(
14-
"When creating a user model":
14+
'When creating a user model':
1515
topic: () ->
1616
new User()
17-
"it should have default values": (user) ->
18-
assert.isUndefined user.name
19-
assert.isUndefined user.userSince
20-
assert.isArray user.slugs
21-
assert.isArray user.handles
22-
assert.isArray user.credentials
23-
"Creating a user model from attributes":
17+
18+
'it should have default values': (user) ->
19+
assert.isUndefined user.attributes.name
20+
assert.isUndefined user.attributes.userSince
21+
assert.isEmpty user.attributes.slugs
22+
assert.isEmpty user.attributes.handles
23+
assert.isEmpty user.attributes.credentials
24+
assert.isUndefined user.attributes._id
25+
26+
'and toJSON is called on the new user with a name.':
27+
topic: (user) ->
28+
user.attributes.name = expectedAttributes.name
29+
user.toJSON()
30+
31+
'it should have the users name populated.': (jsonResult) ->
32+
assert.equal jsonResult.name, expectedAttributes.name
33+
34+
'it should not have any slugs': (jsonResult) ->
35+
assert.isEmpty jsonResult.slugs
36+
37+
'it should not have any handles': (jsonResult) ->
38+
assert.isEmpty jsonResult.handles
39+
40+
'it should not have any credentials': (jsonResult) ->
41+
assert.isEmpty jsonResult.credentials
42+
43+
'it should not have the user since date set': (jsonResult)->
44+
assert.isUndefined jsonResult.userSince
45+
46+
'it should not set an id': (jsonResult) ->
47+
assert.isUndefined jsonResult._id
48+
49+
'and a brand new credential is added':
50+
topic: () ->
51+
user = new User()
52+
user.setCredential 'twitter', 1865
53+
user
54+
55+
'it should add the credential': (user) ->
56+
assert.equal user.attributes.credentials.length, 1
57+
assert.equal user.attributes.credentials[0].provider, 'twitter'
58+
assert.equal user.attributes.credentials[0].id, 1865
59+
60+
'and the same credential is added again with a different id':
61+
topic: (user) ->
62+
user.setCredential 'twitter', 1234
63+
user
64+
65+
'it should override the existing value': (user) ->
66+
assert.equal user.attributes.credentials.length, 1
67+
assert.equal user.attributes.credentials[0].provider, 'twitter'
68+
assert.equal user.attributes.credentials[0].id, 1234
69+
70+
'and the same credential is added with different casing':
71+
topic: (user) ->
72+
user.setCredential 'Twitter', 1234
73+
user
74+
75+
'it should override the existing value': (user) ->
76+
assert.equal user.attributes.credentials.length, 1
77+
assert.equal user.attributes.credentials[0].provider, 'twitter'
78+
assert.equal user.attributes.credentials[0].id, 1234
79+
80+
'and the same credential is added with different casing and white space':
81+
topic: (user) ->
82+
user.setCredential ' Twitter ', 1234
83+
user
84+
85+
'it should override the existing value': (user) ->
86+
console.log user.attributes.credentials
87+
assert.equal user.attributes.credentials.length, 1
88+
assert.equal user.attributes.credentials[0].provider, 'twitter'
89+
assert.equal user.attributes.credentials[0].id, 1234
90+
91+
'and a completely different credential is added':
92+
topic: (user) ->
93+
user.setCredential 'github', 100
94+
user
95+
96+
'it should be appended to the credentials': (user) ->
97+
assert.equal user.attributes.credentials.length, 2
98+
assert.equal user.attributes.credentials[1].provider, 'github'
99+
100+
'Creating a user model from attributes':
24101
topic: () ->
25-
new User expectedAttributes
102+
user = new User expectedAttributes
103+
104+
'it should set the correct name': (user) ->
105+
assert.equal user.attributes.name, expectedAttributes.name
26106

27-
"it should set the correct name": (user) ->
28-
assert.equal user.name, expectedAttributes.name
107+
'it should set the correct user since date': (user) ->
108+
assert.equal user.attributes.userSince, expectedAttributes.userSince
29109

30-
"it should set the correct user since date": (user) ->
31-
assert.equal user.userSince, expectedAttributes.userSince
110+
'it should set the correct slugs': (user) ->
111+
assert.deepEqual user.attributes.slugs, expectedAttributes.slugs
32112

33-
"it should set the correct slugs": (user) ->
34-
assert.deepEqual user.slugs, expectedAttributes.slugs
113+
'it should set the correct handles': (user) ->
114+
assert.deepEqual user.attributes.handles, expectedAttributes.handles
35115

36-
"it should set the correct handles": (user) ->
37-
assert.deepEqual user.handles, expectedAttributes.handles
116+
'it should set the correct credentials': (user) ->
117+
assert.deepEqual user.attributes.credentials, expectedAttributes.credentials
38118

39-
"it should set the correct credentials": (user) ->
40-
assert.deepEqual user.credentials, expectedAttributes.credentials
119+
'it should set the correct id': (user) ->
120+
assert.equal user.attributes._id, expectedAttributes._id
41121

42-
"it should set the correct id": (user) ->
43-
assert.equal user._id, expectedAttributes._id
122+
'and toJSON() is called':
123+
topic: (user) ->
124+
user.toJSON()
44125

126+
'it should set the correct name': (jsonResult) ->
127+
assert.equal jsonResult.name, expectedAttributes.name
128+
129+
'it should set the correct slugs': (jsonResult) ->
130+
assert.deepEqual jsonResult.slugs, expectedAttributes.slugs
131+
132+
'it should set the correct handles': (jsonResult) ->
133+
assert.deepEqual jsonResult.handles, expectedAttributes.handles
134+
135+
'it should set the correct credentials': (jsonResult) ->
136+
assert.deepEqual jsonResult.credentials, expectedAttributes.credentials
137+
138+
'it should set the correct id': (jsonResult) ->
139+
assert.equal jsonResult._id, expectedAttributes._id
140+
45141
).export module

0 commit comments

Comments
 (0)