forked from AlexITC/crypto-coin-alerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserPostgresDataHandlerSpec.scala
183 lines (147 loc) · 6.83 KB
/
UserPostgresDataHandlerSpec.scala
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.alexitc.coinalerts.data.anorm
import com.alexitc.coinalerts.commons.{PostgresDataHandlerSpec, RandomDataGenerator}
import com.alexitc.coinalerts.data.anorm.dao.UserPostgresDAO
import com.alexitc.coinalerts.errors._
import com.alexitc.coinalerts.models._
import org.scalactic.{Bad, Good, One}
import play.api.i18n.Lang
class UserPostgresDataHandlerSpec extends PostgresDataHandlerSpec {
lazy val userPostgresDataHandler = new UserPostgresDataHandler(database, new UserPostgresDAO)
"Creating a user" should {
"Allow to create a new user" in {
val email = RandomDataGenerator.email
val password = RandomDataGenerator.hiddenPassword
val result = userPostgresDataHandler.create(email, password)
result.isGood mustEqual true
val user = result.get
user.email mustEqual email
}
"Fail to create a new user when the email already exists" in {
val email = RandomDataGenerator.email
val password = RandomDataGenerator.hiddenPassword
userPostgresDataHandler.create(email, password).isGood mustEqual true
val result = userPostgresDataHandler.create(email.copy(string = email.string.toUpperCase), password)
result mustEqual Bad(EmailAlreadyExistsError).accumulating
}
}
"Creating a verification token" should {
"Allow to create a verification token" in {
val email = RandomDataGenerator.email
val userId = userPostgresDataHandler.create(email, RandomDataGenerator.hiddenPassword).get.id
val tokenResult = userPostgresDataHandler.createVerificationToken(userId)
tokenResult.isGood mustEqual true
}
"Fail to create a verification token for a user that already has one" in {
val email = RandomDataGenerator.email
val userId = userPostgresDataHandler.create(email, RandomDataGenerator.hiddenPassword).get.id
userPostgresDataHandler.createVerificationToken(userId).isGood mustEqual true
userPostgresDataHandler
.createVerificationToken(userId) mustEqual Bad(UserVerificationTokenAlreadyExistsError).accumulating
}
"Fail to create a verification token for an unknown user" in {
val userId = new UserId("no-one")
val tokenResult = userPostgresDataHandler.createVerificationToken(userId)
tokenResult.isBad mustEqual true
tokenResult.swap.get.head.isInstanceOf[PostgresIntegrityViolationError] mustEqual true
}
}
"Verifying a user token" should {
"Allow to verify user email by token" in {
val userId = userPostgresDataHandler.create(RandomDataGenerator.email, RandomDataGenerator.hiddenPassword).get.id
val token = userPostgresDataHandler.createVerificationToken(userId).get
val result = userPostgresDataHandler.verifyEmail(token)
result.isGood mustEqual true
result.get.id mustEqual userId
}
"Fail to verify user email given an invalid token" in {
val token = new UserVerificationToken("no-one")
val result = userPostgresDataHandler.verifyEmail(token)
result mustEqual Bad(UserVerificationTokenNotFoundError).accumulating
}
}
"Retrieving user password" should {
"Allow to retrieve the passsword" in {
val email = RandomDataGenerator.email
val password = RandomDataGenerator.hiddenPassword
val _ = createVerifiedUser(email, password)
val result = userPostgresDataHandler.getVerifiedUserPassword(email)
result mustEqual Good(password)
}
"Fail to retrieve the password for an unknown user" in {
val result = userPostgresDataHandler.getVerifiedUserPassword(RandomDataGenerator.email)
result mustEqual Bad(One(VerifiedUserNotFound))
}
"Fail to retrieve the password for an unverified user" in {
val email = RandomDataGenerator.email
userPostgresDataHandler.create(email, RandomDataGenerator.hiddenPassword)
val result = userPostgresDataHandler.getVerifiedUserPassword(email)
result mustEqual Bad(One(VerifiedUserNotFound))
}
}
"Retrieving a user by email" should {
"Allow to retrieve the user" in {
val email = RandomDataGenerator.email
val user = createVerifiedUser(email)
val result = userPostgresDataHandler.getVerifiedUserByEmail(email)
result mustEqual Good(User(user.id, email))
}
"Fail to retrieve an unknown user" in {
val result = userPostgresDataHandler.getVerifiedUserByEmail(RandomDataGenerator.email)
result mustEqual Bad(One(VerifiedUserNotFound))
}
"Fail to retrieve an unverified user" in {
val email = RandomDataGenerator.email
userPostgresDataHandler.create(email, RandomDataGenerator.hiddenPassword)
val result = userPostgresDataHandler.getVerifiedUserByEmail(email)
result mustEqual Bad(One(VerifiedUserNotFound))
}
}
"Retrieving a user by id" should {
"Allow to retrieve the user" in {
val email = RandomDataGenerator.email
val user = createVerifiedUser(email)
val result = userPostgresDataHandler.getVerifiedUserById(user.id)
result mustEqual Good(user)
}
"Fail to retrieve an unknown user" in {
val result = userPostgresDataHandler.getVerifiedUserById(UserId.create)
result mustEqual Bad(One(VerifiedUserNotFound))
}
"Fail to retrieve an unverified user" in {
val email = RandomDataGenerator.email
val user = userPostgresDataHandler.create(email, RandomDataGenerator.hiddenPassword).get
val result = userPostgresDataHandler.getVerifiedUserById(user.id)
result mustEqual Bad(One(VerifiedUserNotFound))
}
}
"Retrieving user preferences" should {
"Succeed when the user exists" in {
val user = createVerifiedUser(RandomDataGenerator.email)
val preferences = userPostgresDataHandler.getUserPreferences(user.id)
preferences.isGood mustEqual true
}
"Succeed even if the user doesn't exists" in {
val preferences = userPostgresDataHandler.getUserPreferences(UserId.create)
preferences.isGood mustEqual true
}
}
"Setting user preferences" should {
"update the preferences" in {
val user = createVerifiedUser(RandomDataGenerator.email)
val lang = Lang("es")
val preferencesModel = SetUserPreferencesModel.default.copy(lang = lang)
val result = userPostgresDataHandler.setUserPreferences(user.id, preferencesModel).get
result.lang mustEqual lang
}
"fail when the user doesn't exist" in {
val preferencesModel = SetUserPreferencesModel.default
val result = userPostgresDataHandler.setUserPreferences(UserId.create, preferencesModel)
result mustEqual Bad(VerifiedUserNotFound).accumulating
}
}
def createVerifiedUser(email: UserEmail, password: UserHiddenPassword = RandomDataGenerator.hiddenPassword) = {
val user = userPostgresDataHandler.create(email, password).get
val token = userPostgresDataHandler.createVerificationToken(user.id).get
userPostgresDataHandler.verifyEmail(token).get
}
}