-
Notifications
You must be signed in to change notification settings - Fork 3
/
UserRegistrationValidationSpec.groovy
126 lines (105 loc) · 4.55 KB
/
UserRegistrationValidationSpec.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
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
package com.rg.webmvctest.rest
import com.fasterxml.jackson.databind.ObjectMapper
import com.rg.webmvctest.BaseWebMvcSpec
import com.rg.webmvctest.domain.RegistrationService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.http.HttpStatus
import spock.lang.Unroll
import spock.mock.DetachedMockFactory
import static groovy.json.JsonOutput.toJson
import static org.springframework.http.MediaType.APPLICATION_JSON
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
@WebMvcTest(controllers = [UserRegistrationController])
class UserRegistrationValidationSpec extends BaseWebMvcSpec {
@Autowired
ObjectMapper objectMapper
@Unroll
def "should not allow to create a registration with an invalid email address: #emailAddress"() {
given:
Map request = [
email_address : emailAddress,
name : 'John',
last_name : 'Wayne'
]
when:
def results = doRequest(
post('/registrations').contentType(APPLICATION_JSON).content(toJson(request))
)
then:
results.andExpect(status().isUnprocessableEntity())
and:
results.andExpect(jsonPath('$.errors[0].code').value('MethodArgumentNotValidException'))
results.andExpect(jsonPath('$.errors[0].path').value('emailAddress'))
results.andExpect(jsonPath('$.errors[0].userMessage').value(userMessage))
where:
emailAddress || userMessage
'john.wayne(at)gmail.com' || 'Invalid email address.'
'abcdefg' || 'Invalid email address.'
'' || 'Invalid email address.'
null || 'Email must be provided.'
}
@Unroll
def "should not allow to create a registration with an invalid email address: #emailAddress - spock assertions"() {
given:
Map request = [
email_address : emailAddress,
name : 'John',
last_name : 'Wayne'
]
when:
def result = doRequest(
post('/registrations').contentType(APPLICATION_JSON).content(toJson(request))
).andReturn()
then:
result.response.status == HttpStatus.UNPROCESSABLE_ENTITY.value()
and:
with (objectMapper.readValue(result.response.contentAsString, Map)) {
it.errors[0].code == 'MethodArgumentNotValidException'
it.errors[0].path == 'emailAddress'
it.errors[0].userMessage == userMessage
}
where:
emailAddress || userMessage
'john.wayne(at)gmail.com' || 'Invalid email address.'
'abcdefg' || 'Invalid email address.'
'' || 'Invalid email address.'
null || 'Email must be provided.'
}
@Unroll
def "should not allow to create a registration with an invalid name: #name"() {
given:
Map request = [
email_address : 'john.wayne@gmail.com',
name : name,
last_name : 'Wayne'
]
when:
def results = doRequest(
post('/registrations').contentType(APPLICATION_JSON).content(toJson(request))
)
then:
results.andExpect(status().isUnprocessableEntity())
and:
results.andExpect(jsonPath('$.errors[0].code').value('MethodArgumentNotValidException'))
results.andExpect(jsonPath('$.errors[0].path').value('name'))
results.andExpect(jsonPath('$.errors[0].userMessage').value(userMessage))
where:
name || userMessage
null || 'Name must be provided.'
'I' || 'Name must be at least 2 characters and at most 50 characters long.'
'' || 'Name must be at least 2 characters and at most 50 characters long.'
}
@TestConfiguration
static class StubConfig {
DetachedMockFactory detachedMockFactory = new DetachedMockFactory()
@Bean
RegistrationService registrationService() {
return detachedMockFactory.Stub(RegistrationService)
}
}
}