-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserServiceSpec.groovy
75 lines (60 loc) · 2.25 KB
/
UserServiceSpec.groovy
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
package com.skiwi.businesssoftware
import grails.test.mixin.Mock
import grails.test.mixin.TestFor
import grails.test.mixin.TestMixin
import grails.test.mixin.domain.DomainClassUnitTestMixin
import spock.lang.Specification
import java.time.Clock
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
/**
* See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
*/
@TestFor(UserService)
@Mock([User, Person])
@TestMixin(DomainClassUnitTestMixin)
class UserServiceSpec extends Specification {
static nowInstant = Instant.now()
static doWithSpring = {
clock(Clock) { bean ->
bean.factoryMethod = "fixed"
bean.constructorArgs = [nowInstant, ZoneId.systemDefault()]
}
}
def "test create user"() {
when: "create user"
def user = service.createUser("John", null, "Doe", "john@doe.com", "johndoe")
then: "user is saved and properties are correct"
user
def userDb = User.findByEmail("john@doe.com")
userDb.email == "john@doe.com"
userDb.password != "johndoe"
userDb.registrationDate == nowInstant
userDb.person.firstName == "John"
userDb.person.middleName == null
userDb.person.lastName == "Doe"
when: "create another user with the same email"
service.createUser("Jane", null, "Doe", "john@doe.com", "janedoe")
then: "email is already in use"
def ex = thrown(CreateUserException)
ex.message == "EMAIL_IN_USE"
}
def "test login user"() {
service.createUser("John", null, "Doe", "john@doe.com", "johndoe")
when: "login with non-existing email"
service.loginUser("info@doe.com", "johndoe")
then: "email is not found"
def ex = thrown(LoginUserException)
ex.message == "EMAIL_NOT_FOUND"
when: "login with incorrect password"
service.loginUser("john@doe.com", "infodoe")
then: "password is incorrect"
def ex2 = thrown(LoginUserException)
ex2.message == "PASSWORD_INCORRECT"
when: "login with a valid user"
def user = service.loginUser("john@doe.com", "johndoe")
then: "last login time is set"
user.loginDate == nowInstant
}
}