forked from consuldemocracy/consuldemocracy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature_spec.rb
282 lines (201 loc) · 8.46 KB
/
signature_spec.rb
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
require "rails_helper"
describe Signature do
let(:signature) { build(:signature) }
describe "validations" do
it "is valid" do
expect(signature).to be_valid
end
it "is not valid without a document number" do
signature.document_number = nil
expect(signature).not_to be_valid
signature.document_number = ""
expect(signature).not_to be_valid
signature.document_number = " "
expect(signature).not_to be_valid
end
it "is not valid without an associated signature sheet" do
signature.signature_sheet = nil
expect(signature).not_to be_valid
end
end
describe "custom validations" do
let(:signature) { build(:signature,
document_number: "12345678Z",
date_of_birth: "31/12/1980",
postal_code: "28013") }
before do
Setting["feature.remote_census"] = true
Setting["remote_census.request.date_of_birth"] = "some.value"
Setting["remote_census.request.postal_code"] = "some.value"
end
it "is valid" do
expect(signature).to be_valid
end
it "is not valid without a document number" do
signature.document_number = nil
expect(signature).not_to be_valid
end
it "is not valid without a date of birth" do
signature.date_of_birth = nil
expect(signature).not_to be_valid
end
it "is not valid without a postal_code" do
signature.postal_code = nil
expect(signature).not_to be_valid
end
end
describe "#clean_document_number" do
it "removes non alphanumeric characters" do
signature = create(:signature, document_number: "123-[;,9]")
expect(signature.document_number).to eq("1239")
end
it "upcases letter in document number" do
signature = create(:signature, document_number: "123a")
expect(signature.document_number).to eq("123A")
end
it "deals gracefully with empty document numbers" do
signature = build(:signature, document_number: "")
signature.clean_document_number
expect(signature.document_number).to eq("")
end
end
describe "#verify" do
describe "existing user" do
it "assigns vote to user on proposal" do
user = create(:user, :level_two, document_number: "123A")
signature = create(:signature, document_number: user.document_number)
proposal = signature.signable
signature.verify
expect(user.voted_for?(proposal)).to be
end
it "assigns vote to user on budget investment" do
investment = create(:budget_investment)
signature_sheet = create(:signature_sheet, signable: investment)
user = create(:user, :level_two, document_number: "123A")
signature = create(:signature, document_number: user.document_number, signature_sheet: signature_sheet)
signature.verify
expect(user.voted_for?(investment)).to be
end
it "does not assign vote to user multiple times" do
user = create(:user, :level_two, document_number: "123A")
signature = create(:signature, document_number: user.document_number)
signature.verify
signature.verify
expect(Vote.count).to eq(1)
end
it "does not assigns vote to invalid user on budget investment" do
investment = create(:budget_investment)
signature_sheet = create(:signature_sheet, signable: investment)
user = create(:user, document_number: "123A")
signature = create(:signature, document_number: user.document_number, signature_sheet: signature_sheet)
signature.verify
expect(user.voted_for?(investment)).not_to be
expect(Vote.count).to eq(0)
end
it "does not assign vote to user multiple times on budget investment" do
investment = create(:budget_investment)
signature_sheet = create(:signature_sheet, signable: investment)
user = create(:user, :level_two, document_number: "123A")
signature = create(:signature, document_number: user.document_number, signature_sheet: signature_sheet)
signature.verify
signature.verify
expect(Vote.count).to eq(1)
end
it "does not assign vote to user if already voted" do
proposal = create(:proposal)
user = create(:user, :level_two, document_number: "123A")
vote = create(:vote, votable: proposal, voter: user)
signature_sheet = create(:signature_sheet, signable: proposal)
signature = create(:signature, signature_sheet: signature_sheet, document_number: user.document_number)
signature.verify
expect(Vote.count).to eq(1)
end
it "does not assign vote to user if already voted on budget investment" do
investment = create(:budget_investment)
user = create(:user, :level_two, document_number: "123A")
vote = create(:vote, votable: investment, voter: user)
signature_sheet = create(:signature_sheet, signable: investment)
signature = create(:signature, document_number: user.document_number, signature_sheet: signature_sheet)
expect(Vote.count).to eq(1)
signature.verify
expect(Vote.count).to eq(1)
end
it "marks the vote as coming from a signature" do
signature = create(:signature, document_number: "12345678Z")
signature.verify
expect(Vote.last.signature).to eq(signature)
end
end
describe "inexistent user" do
it "creates a user with that document number" do
create(:geozone, census_code: "01")
signature = create(:signature, document_number: "12345678Z")
signature.verify
user = User.last
expect(user.document_number).to eq("12345678Z")
expect(user.created_from_signature).to eq(true)
expect(user.verified_at).to be
expect(user.erased_at).to be
expect(user.geozone).to be
expect(user.gender).to be
expect(user.date_of_birth).to be
end
it "assign the vote to newly created user" do
signature = create(:signature, document_number: "12345678Z")
proposal = signature.signable
signature.verify
user = signature.user
expect(user.voted_for?(proposal)).to be
end
it "assigns signature to vote" do
signature = create(:signature, document_number: "12345678Z")
signature.verify
expect(Vote.last.signature).to eq(signature)
end
end
describe "document in census" do
it "calls assign_vote_to_user" do
signature = create(:signature, document_number: "12345678Z")
allow(signature).to receive(:assign_vote_to_user)
signature.verify
end
it "sets signature as verified" do
user = create(:user, :level_two, document_number: "123A")
signature = create(:signature, document_number: user.document_number)
signature.verify
expect(signature).to be_verified
end
end
describe "document in census throught CustomCensusApi" do
before do
Setting["feature.remote_census"] = true
Setting["remote_census.request.date_of_birth"] = "some.value"
Setting["remote_census.request.postal_code"] = "some.value"
access_user_data = "get_habita_datos_response.get_habita_datos_return.datos_habitante.item"
access_residence_data = "get_habita_datos_response.get_habita_datos_return.datos_vivienda.item"
Setting["remote_census.response.date_of_birth"] = "#{access_user_data}.fecha_nacimiento_string"
Setting["remote_census.response.postal_code"] = "#{access_residence_data}.codigo_postal"
Setting["remote_census.response.valid"] = access_user_data
end
it "calls assign_vote_to_user" do
signature = create(:signature, document_number: "12345678Z",
date_of_birth: "31/12/1980",
postal_code: "28013")
expect_any_instance_of(Signature).to receive(:assign_vote_to_user).exactly(1).times
signature.verify
end
end
describe "document not in census" do
it "does not call assign_vote_to_user" do
signature = create(:signature, document_number: "123A")
expect(signature).not_to receive(:assign_vote_to_user)
signature.verify
end
it "maintains signature as not verified" do
signature = create(:signature, document_number: "123A")
signature.verify
expect(signature).not_to be_verified
end
end
end
end