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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
require_relative "../../test_helper"
require "rack/test"
require "pro_tacts/web"
# The group screens, exercised the way AdminContactsPagesTest exercises
# a contact's: real requests through the Roda app, against a throwaway
# store.
class AdminGroupsPagesTest < Minitest::Test
include Rack::Test::Methods
include ThrowawayContacts
def app
ProTacts::Web
end
def setup
header "Remote-User", "test@example.com"
end
ADDRESS = "ADR;TYPE=home:;;7 Calculus Close;London;England;NW1 1AB;United Kingdom" #: String
NOTE = "NOTE:Gate code 1854." #: String
# Contacts by id and name, each a bare card.
def with_contacts(names, &) = super(names.to_h { |id, name| [id, card(id, name)] }, &)
def household(store, members: %w[george mary])
id = store.create_group(name: "Booles")
store.set_group_lines(id, [ADDRESS, NOTE])
members.each { store.add_member(id, it) }
id
end
BOOLES = {"george" => "George Boole", "mary" => "Mary Boole", "ada" => "Ada Lovelace"}.freeze
## The list
def test_the_list_names_every_group_and_its_size
with_contacts(BOOLES) do |store|
id = household(store)
nameless = store.create_group
get "/groups"
assert_equal 200, last_response.status
assert_includes last_response.body, %(<a href="/groups/#{id}">)
assert_includes last_response.body, "Booles"
assert_includes last_response.body, "2 members"
assert_includes last_response.body, nameless
end
end
# The `sync:` groups are the books a client syncs, so they lead the
# list; the rest read alphabetically, whatever their case.
def test_the_list_leads_with_the_sync_groups_and_then_reads_alphabetically
with_contacts({}) do |store|
%w[zoetrope Abacus sync:alpha barometer sync:*].each { store.create_group(name: it) }
ids = store.all_groups.to_h { [it.name, it.id] }
get "/groups"
assert_equal %w[sync:* sync:alpha Abacus barometer zoetrope].map { ids.fetch(it) },
last_response.body.scan(%r{<a href="/groups/([^"]+)">}).flatten
end
end
# A nameless group is labelled by its id, which reads as noise among
# names, so those sink below the named ones.
def test_the_list_ends_with_the_nameless_groups
with_contacts({}) do |store|
nameless = store.create_group
zoetrope = store.create_group(name: "zoetrope")
abacus = store.create_group(name: "Abacus")
get "/groups"
assert_equal [abacus, zoetrope, nameless],
last_response.body.scan(%r{<a href="/groups/([^"]+)">}).flatten
end
end
def test_an_empty_list_says_so
with_contacts({}) do
get "/groups"
assert_includes last_response.body, "No groups yet."
end
end
def test_the_header_leads_to_the_groups
with_contacts({}) do
get "/"
body = last_response.body
assert_includes body, %(<a href="/">pro-tacts</a>)
assert_includes body, %(<nav><a href="/contacts">contacts</a><a href="/groups">groups</a></nav>)
refute_includes body, %(<a href="/groups" class="type-label">)
end
end
## Creating
# A new group is nothing until something is added to it, so the
# create lands on its editor.
def test_a_create_lands_on_the_new_groups_editor
with_contacts({}) do |store|
post "/groups", name: "Booles"
id = store.all_groups.fetch(0).id
assert_equal 303, last_response.status
assert_equal "/groups/#{id}/edit", URI(last_response["Location"]).path
assert_equal "Booles", store.group(id).name
end
end
def test_a_blank_create_makes_a_nameless_group
with_contacts({}) do |store|
post "/groups", name: " "
assert_nil store.all_groups.fetch(0).name
end
end
# A name is one group's (db/migrations/008_group_names.rb).
def test_a_create_under_a_taken_name_is_refused
with_contacts({}) do |store|
store.create_group(name: "Booles")
post "/groups", name: "Booles"
assert_equal 200, last_response.status
assert_includes last_response.body, "Another group is already named Booles."
assert_equal 1, store.all_groups.size
end
end
## The card
def test_the_card_shows_what_the_group_lends_and_to_whom
with_contacts(BOOLES) do |store|
id = household(store)
get "/groups/#{id}"
body = last_response.body
assert_equal 200, last_response.status
assert_includes body, "7 Calculus Close"
assert_includes body, "Gate code 1854."
assert_includes body, "members (2)"
assert_match %r{<a href="/contacts/george">.*?George Boole</div>}, body
assert_match %r{<a href="/contacts/mary">.*?Mary Boole</div>}, body
assert_includes body, "edit members"
refute_includes body, "Ada Lovelace"
end
end
# The members list the way /contacts does (Contact#<=>), not in
# the store's id order. Seeded so the two disagree. The scan takes
# the list's own rows — an li's link, not the change log's, which
# names the same cards for other reasons — and only from the
# members heading down: the header's account menu carries an li
# link into /contacts too (the export), above every list.
def test_the_members_list_in_name_order
with_contacts(BOOLES.merge("zz" => "Alice Aardvark")) do |store|
id = household(store, members: %w[mary zz george])
get "/groups/#{id}"
members = last_response.body.split("members (").last
assert_equal %w[zz george mary], members.scan(%r{<li><a href="/contacts/([^"]+)">}).flatten
end
end
def test_a_memberless_card_still_offers_to_edit_members
with_contacts(BOOLES) do |store|
id = household(store, members: [])
get "/groups/#{id}"
assert_includes last_response.body, "members (0)"
assert_includes last_response.body, "No members yet."
assert_includes last_response.body, "edit members"
end
end
# A group that lends nothing renders no record grid, so its name is
# not held above an empty one (.detail-header in admin.css) — the
# change log's own grid below does not count, being another card's.
def test_a_card_that_lends_nothing_has_no_grid
with_contacts(BOOLES) do |store|
id = store.create_group(name: "Booles")
get "/groups/#{id}"
refute_includes last_response.body, %(<dl class="detail-grid">)
end
end
def test_an_unknown_group_is_a_404
with_contacts({}) do
get "/groups/zzzz"
assert_equal 404, last_response.status
get "/groups/zzzz/edit"
assert_equal 404, last_response.status
end
end
## The editor
def test_the_editors_save_submits_its_form
with_contacts(BOOLES) do |store|
id = household(store)
get "/groups/#{id}/edit"
assert_includes last_response.body, %(id="group-form")
assert_includes last_response.body, '<button type="submit" form="group-form" data-variant="primary">Save</button>'
end
end
def test_the_editor_leaves_membership_to_the_card
with_contacts(BOOLES) do |store|
id = household(store)
get "/groups/#{id}/edit"
body = last_response.body
assert_includes body, %(<input type="hidden" name="version" value="#{store.group(id).version}">)
refute_includes body, "George Boole"
refute_includes body, "edit members"
refute_includes body, %(name="members[]")
assert_includes body, %(name="address[)
end
end
# The whole save: the name and a line in one POST, and every card
# whose served bytes moved told so in the change log. Membership is
# the members screen's, so a list sent here moves nobody.
def test_a_save_writes_the_name_and_the_lines_but_not_the_members
with_contacts(BOOLES) do |store|
id = household(store)
group = store.group(id)
post "/groups/#{id}", version: group.version, name: "The Booles", note: "Gate code 1912.",
members: ["", "ada"]
assert_equal 303, last_response.status
saved = store.group(id)
assert_equal "The Booles", saved.name
assert_equal [ADDRESS, "NOTE:Gate code 1912."], saved.lines
assert_equal %w[george mary], saved.members
assert_equal "group", store.changes_of("mary").first.action
end
end
def test_a_save_edits_and_adds_an_address
with_contacts(BOOLES) do |store|
id = household(store)
group = store.group(id)
digest = group.reading.addresses.fetch(0).line.digest
post "/groups/#{id}", version: group.version, note: "Gate code 1854.",
address: {digest => {street: "8 Calculus Close", locality: "London",
region: "England", postal_code: "NW1 1AB",
country: "United Kingdom"}},
new_address: {"0" => {street: "1 Long Road"}}
assert_equal [ADDRESS.sub("7 Calculus", "8 Calculus"), NOTE, "ADR:;;1 Long Road;;;;"],
store.group(id).lines
end
end
# The snapshot guard, the contact editor's: a group changed since the
# page loaded refuses the stale save rather than reverting the change.
def test_a_stale_save_is_refused_and_changes_nothing
with_contacts(BOOLES) do |store|
id = household(store)
stale = store.group(id).version
store.rename_group(id, name: "The Booles")
post "/groups/#{id}", version: stale, name: "Booles"
assert_equal 200, last_response.status
assert_includes last_response.body, "This group changed since the page loaded; nothing was saved."
assert_equal "The Booles", store.group(id).name
assert_equal %w[george mary], store.group(id).members
end
end
def test_a_rename_to_a_taken_name_is_refused_and_changes_nothing
with_contacts(BOOLES) do |store|
store.create_group(name: "Clarks")
id = household(store)
post "/groups/#{id}", version: store.group(id).version, name: "Clarks", note: "Gate code 1854."
assert_equal 200, last_response.status
assert_includes last_response.body, "Another group is already named Clarks; nothing was saved."
assert_equal "Booles", store.group(id).name
assert_equal %w[george mary], store.group(id).members
end
end
# Everyone's book keeps its name: renaming it would empty every
# device's book (Store#rename_group). The editor shows a book's name
# as text, so only a POST made by hand names one, and it raises.
def test_a_rename_of_a_books_group_raises_and_changes_nothing
with_contacts(BOOLES) do |store|
id = FixtureData.seed_group(store, name: ProTacts::Group::EVERYONE, members: %w[george])
assert_raises(ProTacts::Store::SyncGroupRename) do
post "/groups/#{id}", version: store.group(id).version, name: "Everyone"
end
assert_equal ProTacts::Group::EVERYONE, store.group(id).name
assert_equal %w[george], store.group(id).members
end
end
def test_a_books_group_shows_its_name_as_text
with_contacts(BOOLES) do |store|
id = FixtureData.seed_group(store, name: ProTacts::Group::EVERYONE, members: %w[george])
get "/groups/#{id}/edit"
refute_includes last_response.body, %(name="name")
assert_includes last_response.body, "<span>Name</span><span>sync:*</span>"
end
end
# The form a book's group renders carries no name, and its save
# keeps the name the group has.
def test_a_save_without_a_name_keeps_it
with_contacts(BOOLES) do |store|
id = FixtureData.seed_group(store, name: ProTacts::Group::EVERYONE, members: %w[george])
post "/groups/#{id}", version: store.group(id).version, note: "Gate code 1854."
assert_equal 303, last_response.status
assert_equal ProTacts::Group::EVERYONE, store.group(id).name
assert_equal ["NOTE:Gate code 1854."], store.group(id).lines
end
end
## The photo
JPEG = "\xFF\xD8\xFF\xE0\x00\x10JFIF\x00\xFF".b #: String
def upload(bytes, type = "image/jpeg")
Rack::Test::UploadedFile.new(StringIO.new(bytes), type, original_filename: "family.jpg")
end
# An upload lands on the group's page, which shows it from the photo
# route; no member's card carries it.
def test_an_uploaded_photo_shows_on_the_groups_page_alone
with_contacts(BOOLES) do |store|
id = household(store)
george = store.contact("george").vcard.to_s
post "/groups/#{id}/photo", photo: upload(JPEG)
assert_equal 303, last_response.status
assert_equal "/groups/#{id}", URI(last_response["Location"]).path
get "/groups/#{id}"
assert_includes last_response.body, %(<img class="group-photo" src="/groups/#{id}/photo" alt="">)
get "/groups"
refute_includes last_response.body, "/photo"
assert_equal george, store.contact("george").vcard.to_s
end
end
def test_the_photo_route_serves_the_bytes
with_contacts({}) do |store|
id = household(store, members: [])
post "/groups/#{id}/photo", photo: upload(JPEG)
get "/groups/#{id}/photo"
assert_equal 200, last_response.status
assert_equal "image/jpeg", last_response["Content-Type"]
assert_equal JPEG, last_response.body.b
# Held by a browser the way a contact's is (Web#serve_photo).
get "/groups/#{id}/photo", {}, "HTTP_IF_NONE_MATCH" => last_response["ETag"]
assert_equal 304, last_response.status
end
end
def test_a_group_without_a_photo_renders_none_and_routes_to_404
with_contacts({}) do |store|
id = household(store, members: [])
get "/groups/#{id}"
refute_includes last_response.body, "group-photo"
get "/groups/#{id}/photo"
assert_equal 404, last_response.status
end
end
# The bytes decide what a file is, not its name or its claimed type.
def test_an_upload_that_is_no_image_is_refused
with_contacts({}) do |store|
id = household(store, members: [])
post "/groups/#{id}/photo", photo: upload("not a picture", "image/jpeg")
assert_equal 200, last_response.status
assert_includes last_response.body, "That file is not a JPEG, PNG, or GIF; nothing was saved."
refute store.group(id).photo
end
end
def test_an_upload_over_the_limit_is_refused
with_contacts({}) do |store|
id = household(store, members: [])
post "/groups/#{id}/photo", photo: upload(JPEG + ("\x00" * ProTacts::Web::GROUP_PHOTO_LIMIT).b)
assert_includes last_response.body, "That photo is over 10 MB; nothing was saved."
refute store.group(id).photo
end
end
# A member's chips mark the group with a photo, on the contact's
# page and in the contacts list alike; a group without one is bare.
def test_a_groups_chip_marks_its_photo
with_contacts(BOOLES) do |store|
id = household(store)
plain = store.create_group(name: "Logicians")
store.add_member(plain, "george")
post "/groups/#{id}/photo", photo: upload(JPEG)
["/contacts/george", "/contacts"].each do |path|
get path
assert_match %r{<a href="/groups/#{id}" class="tag"><svg[^>]*>.*?</svg>Booles<span class="gl-visually-hidden">has a photo</span></a>},
last_response.body
assert_includes last_response.body, %(<a href="/groups/#{plain}" class="tag">Logicians</a>)
end
end
end
def test_the_editor_offers_an_upload_and_a_removal_once_there_is_one
with_contacts({}) do |store|
id = household(store, members: [])
get "/groups/#{id}/edit"
assert_includes last_response.body, %(enctype="multipart/form-data")
refute_includes last_response.body, "remove photo"
post "/groups/#{id}/photo", photo: upload(JPEG)
get "/groups/#{id}/edit"
assert_includes last_response.body, "remove photo"
post "/groups/#{id}/photo", remove: "1"
assert_equal 303, last_response.status
refute store.group(id).photo
end
end
## The members screen
def test_the_card_links_to_the_members_screen
with_contacts(BOOLES) do |store|
id = household(store)
get "/groups/#{id}"
assert_includes last_response.body, %(<a href="/groups/#{id}/members" class="type-label">edit members</a>)
end
end
def test_the_screen_ticks_the_members_and_only_them
with_contacts(BOOLES) do |store|
id = household(store)
get "/groups/#{id}/members"
body = last_response.body
assert_equal 200, last_response.status
assert_includes body, %(placeholder="Filter contacts")
# The clear empties the state the rows read, not just the box.
assert_includes body, %(aria-label="Clear filter")
assert_includes body, %(@click="filter = ''")
%w[george mary].each do |member|
assert_includes body, %(name="members[]" value="#{member}" checked)
assert_includes body, %(name="was[]" value="#{member}")
end
assert_includes body, %(<input type="checkbox" name="members[]" value="ada">)
refute_includes body, %(name="was[]" value="ada")
# Every row carries the name a person filters by, nickname and all
assert_includes body, %(data-label="george boole")
assert_includes body, %(data-label="ada lovelace")
end
end
# The client-side filter folds the same way the dashboard's does
# (Format.search_key on the label, the same fold in JS on the
# needle), so an accent hides a row from neither.
def test_the_members_filter_folds_accents
with_contacts({"emile" => "Émile Muñoz"}) do |store|
id = store.create_group
get "/groups/#{id}/members"
assert_includes last_response.body, %(data-label="emile munoz")
assert_includes last_response.body, "normalize('NFD')"
end
end
def test_the_members_list_in_name_order_on_the_screen_too
with_contacts(BOOLES.merge("zz" => "Alice Aardvark")) do |store|
id = household(store, members: %w[mary zz george])
get "/groups/#{id}/members"
assert_equal ["ada lovelace", "alice aardvark", "george boole", "mary boole"],
last_response.body.scan(%r{<li data-label="([^"]+)"}).flatten
end
end
# The whole save: only what was toggled moves, and every card whose
# served bytes moved is told so in the change log.
def test_a_save_moves_only_what_was_toggled
with_contacts(BOOLES) do |store|
id = household(store)
post "/groups/#{id}/members", members: ["george", "ada"], was: %w[george mary]
assert_equal 303, last_response.status
assert_equal "/groups/#{id}", URI(last_response["Location"]).path
assert_equal %w[ada george], store.group(id).members.sort
assert_equal "group", store.changes_of("mary").first.action
assert_equal "group", store.changes_of("ada").first.action
end
end
# The `was[]` posture: a membership moved elsewhere since the page
# loaded is in neither list, and the save says nothing about it.
def test_a_save_leaves_a_membership_moved_elsewhere_alone
with_contacts(BOOLES) do |store|
id = household(store, members: %w[george])
store.add_member(id, "mary")
post "/groups/#{id}/members", members: %w[george], was: %w[george]
assert_equal 303, last_response.status
assert_equal %w[george mary], store.group(id).members.sort
end
end
def test_a_save_ignores_ids_that_name_no_card
with_contacts(BOOLES) do |store|
id = household(store)
post "/groups/#{id}/members", members: %w[george nobody], was: %w[george mary]
assert_equal 303, last_response.status
assert_equal %w[george], store.group(id).members
end
end
def test_an_empty_book_says_so_on_the_members_screen
with_contacts({}) do |store|
id = store.create_group(name: "Booles")
get "/groups/#{id}/members"
assert_includes last_response.body, "No contacts yet."
end
end
# The cap and its lifting (Admin::GroupFilter), over contacts: the
# ninth row stands hidden until asked for, and a member is never
# capped, however far down the book it sorts.
def test_a_long_book_opens_capped_and_offers_the_rest
names = ("A".."I").each_with_index.to_h { |letter, i| ["p#{i}", "Person #{letter}"] }
with_contacts(names) do |store|
id = store.create_group(name: "Booles")
store.add_member(id, "p8")
get "/groups/#{id}/members"
body = last_response.body
assert_includes body, %(<li data-label="person i" :hidden="!visible($el)">)
assert_equal 1, body.scan("data-capped").length
assert_includes body, "show all 9 contacts"
end
end
def test_an_unknown_group_is_a_404_on_the_members_screen
with_contacts({}) do
get "/groups/zzzz/members"
assert_equal 404, last_response.status
post "/groups/zzzz/members", members: []
assert_equal 404, last_response.status
end
end
## The change log
# The contact page's third card in this page's own shape: the
# writes that made the group, under the members it holds — the
# lines entry as the contact page's diff rows, the joins named and
# linked the way the members list names them.
def test_the_card_shows_the_groups_change_log
with_contacts(BOOLES) do |store|
id = household(store)
get "/groups/#{id}"
body = last_response.body
assert_equal 200, last_response.status
assert_includes body, "change log"
assert_includes body, "+#{ADDRESS}"
assert_includes body, "+#{NOTE}"
assert_match %r{<a href="/contacts/mary">Mary Boole</a>}, body
assert_match %r{<a href="/contacts/george">George Boole</a>}, body
end
end
def test_the_change_log_marks_a_rename_and_a_leave
with_contacts(BOOLES) do |store|
id = household(store)
store.rename_group(id, name: "The Booles")
store.remove_member(id, "mary")
get "/groups/#{id}"
body = last_response.body
assert_includes body, "Booles → The Booles"
assert_match %r{<a href="/contacts/mary">Mary Boole</a>}, body
end
end
# The tombstone's text: a join's card since deleted has nothing
# left to link to, so its id is the row.
def test_the_change_log_names_a_gone_card_by_its_id
with_contacts(BOOLES) do |store|
id = household(store, members: ["ada"])
store.delete("ada")
get "/groups/#{id}"
assert_includes last_response.body, "<div>ada</div>"
end
end
## Search
def test_search_finds_a_group_by_its_name
with_contacts(BOOLES) do |store|
id = household(store)
get "/search/results", q: "boole"
assert_includes last_response.body, %(<a href="/groups/#{id}">)
end
end
def test_search_finds_a_contact_by_its_groups_name
with_contacts({"george" => "George", "ada" => "Ada Lovelace"}) do |store|
household(store, members: ["george"])
get "/search/results", q: "boole"
assert_includes last_response.body, %(<a href="/contacts/george">)
refute_includes last_response.body, %(<a href="/contacts/ada">)
end
end
# The fold reaches a group's own name and, through it, the group's
# members: the label matches folded on both sides.
def test_search_folds_accents_in_a_groups_name
with_contacts(BOOLES) do |store|
id = store.create_group(name: "Família")
store.add_member(id, "george")
get "/search/results", q: "familia"
assert_includes last_response.body, %(<a href="/groups/#{id}">)
assert_includes last_response.body, %(<a href="/contacts/george">)
end
end
end