This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.py
1550 lines (1213 loc) · 67.7 KB
/
test.py
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -- coding: utf-8 --
from unittest import main, TestCase
from tempfile import mkdtemp
from StringIO import StringIO
from os.path import join, exists, dirname
from urlparse import urlparse, urljoin
from os import environ
from shutil import rmtree, copytree
from uuid import uuid4
from re import search
import random
from datetime import date, timedelta, datetime
from dateutil import parser, tz
import sys
import os
here = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(here)
from git import Repo
from box.util.rotunicode import RotUnicode
from httmock import response, HTTMock
from bs4 import BeautifulSoup
from mock import MagicMock
from bizarro import (
create_app, jekyll_functions, repo_functions, edit_functions,
google_api_functions, view_functions, publish
)
import codecs
codecs.register(RotUnicode.search_function)
class TestJekyll (TestCase):
def test_good_files(self):
front = dict(title='Greeting'.encode('rotunicode'))
body, file = u'World: Hello.'.encode('rotunicode'), StringIO()
jekyll_functions.dump_jekyll_doc(front, body, file)
_front, _body = jekyll_functions.load_jekyll_doc(file)
self.assertEqual(_front['title'], front['title'])
self.assertEqual(_body, body)
file.seek(0)
file.read(4) == '---\n'
def test_bad_files(self):
file = StringIO('Missing front matter')
with self.assertRaises(Exception):
jekyll_functions.load_jekyll_doc(file)
class TestViewFunctions (TestCase):
def setUp(self):
repo_path = os.path.dirname(os.path.abspath(__file__)) + '/test-app.git'
temp_repo_dir = mkdtemp(prefix='bizarro-root')
temp_repo_path = temp_repo_dir + '/test-app.git'
copytree(repo_path, temp_repo_path)
self.origin = Repo(temp_repo_path)
self.clone = self.origin.clone(mkdtemp(prefix='bizarro-'))
self.session = dict(email=str(uuid4()))
environ['GIT_AUTHOR_NAME'] = ' '
environ['GIT_COMMITTER_NAME'] = ' '
environ['GIT_AUTHOR_EMAIL'] = self.session['email']
environ['GIT_COMMITTER_EMAIL'] = self.session['email']
def tearDown(self):
rmtree(self.origin.git_dir)
rmtree(self.clone.working_dir)
def test_sorted_paths(self):
''' Ensure files/directories are sorted in alphabetical order, and that
we get the expected values back from the sorted_paths method
'''
sorted_list = view_functions.sorted_paths(self.clone, 'master')
now_utc = datetime.utcnow()
now_utc = now_utc.replace(tzinfo=tz.tzutc())
expected_dates = [
'Sat Mar 15 00:55:52 2014 -0700',
'Fri Aug 29 17:58:25 2014 -0700',
'Fri Aug 29 17:58:25 2014 -0700',
'Sat Mar 15 00:55:52 2014 -0700'
]
expected_datetimes = [parser.parse(item) for item in expected_dates]
expected_relative_dates = [view_functions.get_relative_date_string(item, now_utc) for item in expected_datetimes]
expected_list = [('index.md', '/tree/master/view/index.md', 'file', True, expected_relative_dates[0]),
('other', '/tree/master/view/other', 'folder', False, expected_relative_dates[1]),
('other.md', '/tree/master/view/other.md', 'file', True, expected_relative_dates[2]),
('sub', '/tree/master/view/sub', 'folder', False, expected_relative_dates[3])]
self.assertEqual(sorted_list, expected_list)
def test_directory_paths_with_no_relative_path(self):
''' Ensure that a list with pairs of a sub-directory and the absolute path
to that directory is returned for all sub-directories in a path
'''
dirs_and_paths = view_functions.directory_paths('my-branch')
self.assertEqual(dirs_and_paths, [('root', '/tree/my-branch/edit')])
def test_directory_paths_with_relative_path(self):
''' Ensure that a list with pairs of a sub-directory and the absolute path
to that directory is returned for all sub-directories in a path
'''
dirs_and_paths = view_functions.directory_paths('my-branch', 'blah/foo/')
self.assertEqual(dirs_and_paths, [('root', '/tree/my-branch/edit'),
('blah', '/tree/my-branch/edit/blah/'),
('foo', '/tree/my-branch/edit/blah/foo/')])
def test_auth_url(self):
'''
'''
auth_url = 'data/authentication.csv'
csv_url = view_functions.get_auth_csv_url(auth_url)
self.assertEqual(csv_url, auth_url)
auth_url = 'https://docs.google.com/spreadsheets/d/12jUfaRBd-CU1_6BGeLFG1_qoi7Fw_vRC_SXv36eDzM0/edit'
csv_url = view_functions.get_auth_csv_url(auth_url)
self.assertEqual(csv_url, 'https://docs.google.com/spreadsheets/d/12jUfaRBd-CU1_6BGeLFG1_qoi7Fw_vRC_SXv36eDzM0/export?format=csv')
def test_is_allowed_email(self):
'''
'''
def mock_remote_authentication_file(url, request):
if 'good-file.csv' in url.geturl():
return response(200, '''
Some junk below
Email domain,Organization,Email address,Organization,Name
codeforamerica.org,Code for America,mike@teczno.com,Code for America,Mike Migurski
*@codeforamerica.org,Code for America,,,
''')
if 'org-file.csv' in url.geturl():
return response(200, '''
Some junk below
Email domain,Organization
codeforamerica.org,Code for America
*@codeforamerica.org,Code for America
''')
if 'addr-file.csv' in url.geturl():
return response(200, '''
Some junk below
Email address,Organization,Name
mike@teczno.com,Code for America,Mike Migurski
''')
return response(404, '')
good_file = lambda: view_functions.get_auth_data_file('http://example.com/good-file.csv')
org_file = lambda: view_functions.get_auth_data_file('http://example.com/org-file.csv')
addr_file = lambda: view_functions.get_auth_data_file('http://example.com/addr-file.csv')
no_file = lambda: view_functions.get_auth_data_file('http://example.com/no-file.csv')
with HTTMock(mock_remote_authentication_file):
self.assertTrue(view_functions.is_allowed_email(good_file(), 'mike@codeforamerica.org'))
self.assertTrue(view_functions.is_allowed_email(good_file(), 'frances@codeforamerica.org'))
self.assertTrue(view_functions.is_allowed_email(good_file(), 'mike@teczno.com'))
self.assertFalse(view_functions.is_allowed_email(good_file(), 'whatever@teczno.com'))
self.assertTrue(view_functions.is_allowed_email(org_file(), 'mike@codeforamerica.org'))
self.assertTrue(view_functions.is_allowed_email(org_file(), 'frances@codeforamerica.org'))
self.assertFalse(view_functions.is_allowed_email(org_file(), 'mike@teczno.com'))
self.assertFalse(view_functions.is_allowed_email(org_file(), 'whatever@teczno.com'))
self.assertFalse(view_functions.is_allowed_email(addr_file(), 'mike@codeforamerica.org'))
self.assertFalse(view_functions.is_allowed_email(addr_file(), 'frances@codeforamerica.org'))
self.assertTrue(view_functions.is_allowed_email(addr_file(), 'mike@teczno.com'))
self.assertFalse(view_functions.is_allowed_email(addr_file(), 'whatever@teczno.com'))
self.assertFalse(view_functions.is_allowed_email(no_file(), 'mike@codeforamerica.org'))
self.assertFalse(view_functions.is_allowed_email(no_file(), 'frances@codeforamerica.org'))
self.assertFalse(view_functions.is_allowed_email(no_file(), 'mike@teczno.com'))
self.assertFalse(view_functions.is_allowed_email(no_file(), 'whatever@teczno.com'))
class TestRepo (TestCase):
def setUp(self):
repo_path = os.path.dirname(os.path.abspath(__file__)) + '/test-app.git'
temp_repo_dir = mkdtemp(prefix='bizarro-root')
temp_repo_path = temp_repo_dir + '/test-app.git'
copytree(repo_path, temp_repo_path)
self.origin = Repo(temp_repo_path)
self.clone1 = self.origin.clone(mkdtemp(prefix='bizarro-'))
self.clone2 = self.origin.clone(mkdtemp(prefix='bizarro-'))
self.session = dict(email=str(uuid4()))
environ['GIT_AUTHOR_NAME'] = ' '
environ['GIT_COMMITTER_NAME'] = ' '
environ['GIT_AUTHOR_EMAIL'] = self.session['email']
environ['GIT_COMMITTER_EMAIL'] = self.session['email']
def tearDown(self):
rmtree(self.origin.git_dir)
rmtree(self.clone1.working_dir)
rmtree(self.clone2.working_dir)
def test_repo_features(self):
self.assertTrue(self.origin.bare)
branch_names = [b.name for b in self.origin.branches]
self.assertEqual(set(branch_names), set(['master', 'title', 'body']))
def test_start_branch(self):
''' Make a simple edit in a clone, verify that it appears in the other.
'''
name = str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', name)
self.assertTrue(name in self.clone1.branches)
self.assertTrue(name in self.origin.branches)
#
# Make a change to the branch and push it.
#
branch1.checkout()
message = str(uuid4())
with open(join(self.clone1.working_dir, 'index.md'), 'a') as file:
file.write('\n\n...')
args = self.clone1, 'index.md', message, branch1.commit.hexsha, 'master'
repo_functions.save_working_file(*args)
#
# See if the branch made it to clone 2
#
branch2 = repo_functions.start_branch(self.clone2, 'master', name)
self.assertTrue(name in self.clone2.branches)
self.assertEquals(branch2.commit.hexsha, branch1.commit.hexsha)
self.assertEquals(branch2.commit.message, message)
def test_start_branch_2(self):
''' Make a simple edit in a clone, verify that it appears in the other.
'''
name = str(uuid4())
#
# Check out both clones.
#
self.clone1.branches.master.checkout()
self.clone2.branches.master.checkout()
#
# Make a change to the first clone and push it.
#
with open(join(self.clone1.working_dir, 'index.md'), 'a') as file:
file.write('\n\n...')
message = str(uuid4())
args = self.clone1, 'index.md', message, self.clone1.commit().hexsha, 'master'
repo_functions.save_working_file(*args)
#
# Origin now has the updated master, but the second clone does not.
#
self.assertEquals(self.clone1.refs['master'].commit.hexsha, self.origin.refs['master'].commit.hexsha)
self.assertNotEquals(self.clone1.refs['master'].commit.hexsha, self.clone2.refs['master'].commit.hexsha)
#
# Now start a branch from the second clone, and look for the new master commit.
#
branch2 = repo_functions.start_branch(self.clone2, 'master', name)
self.assertTrue(name in self.clone2.branches)
self.assertEquals(branch2.commit.hexsha, self.origin.refs['master'].commit.hexsha)
def test_delete_missing_branch(self):
''' Delete a branch in a clone that's still in origin, see if it can be deleted anyway.
'''
name = str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', name)
self.assertTrue(name in self.origin.branches)
self.clone2.git.fetch()
repo_functions.abandon_branch(self.clone2, 'master', name)
self.assertFalse(name in self.origin.branches)
self.assertFalse(name in self.clone2.branches)
self.assertFalse('origin/' + name in self.clone2.refs)
def test_new_file(self):
''' Make a new file and delete an old file in a clone, verify that it appears in the other.
'''
name = str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', name)
self.assertTrue(name in self.clone1.branches)
self.assertTrue(name in self.origin.branches)
#
# Make a new file in the branch and push it.
#
branch1.checkout()
edit_functions.create_new_page(self.clone1, '', 'hello.md',
dict(title='Hello'), 'Hello hello.')
args = self.clone1, 'hello.md', str(uuid4()), branch1.commit.hexsha, 'master'
repo_functions.save_working_file(*args)
#
# Delete an existing file in the branch and push it.
#
message = str(uuid4())
edit_functions.delete_file(self.clone1, '', 'index.md')
args = self.clone1, 'index.md', message, branch1.commit.hexsha, 'master'
repo_functions.save_working_file(*args)
#
# See if the branch made it to clone 2
#
branch2 = repo_functions.start_branch(self.clone2, 'master', name)
self.assertTrue(name in self.clone2.branches)
self.assertEquals(branch2.commit.hexsha, branch1.commit.hexsha)
self.assertEquals(branch2.commit.message, message)
self.assertEquals(branch2.commit.author.email, self.session['email'])
self.assertEquals(branch2.commit.committer.email, self.session['email'])
branch2.checkout()
with open(join(self.clone2.working_dir, 'hello.md')) as file:
front, body = jekyll_functions.load_jekyll_doc(file)
self.assertEquals(front['title'], 'Hello')
self.assertEquals(body, 'Hello hello.')
self.assertFalse(exists(join(self.clone2.working_dir, 'index.md')))
def test_delete_directory(self):
''' Make a new file and directory and delete them.
'''
name = str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', name)
self.assertTrue(name in self.clone1.branches)
self.assertTrue(name in self.origin.branches)
#
# Make a new file in a directory on the branch and push it.
#
branch1.checkout()
edit_functions.create_new_page(self.clone1, 'hello/', 'hello.md',
dict(title='Hello'), 'Hello hello.')
args = self.clone1, 'hello/hello.md', str(uuid4()), branch1.commit.hexsha, 'master'
repo_functions.save_working_file(*args)
#
# Delete the file and folder just created and push the changes.
#
message = str(uuid4())
edit_functions.delete_file(self.clone1, 'hello/', 'hello.md')
args = self.clone1, 'hello/hello.md', message, branch1.commit.hexsha, 'master'
repo_functions.save_working_file(*args)
self.assertFalse(exists(join(self.clone1.working_dir, 'hello/hello.md')))
edit_functions.delete_file(self.clone1, 'hello/', '')
self.assertFalse(exists(join(self.clone1.working_dir, 'hello/')))
def test_move_file(self):
''' Change the path of a file.
'''
name = str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', name)
self.assertTrue(name in self.clone1.branches)
self.assertTrue(name in self.origin.branches)
#
# Rename a file in the branch.
#
branch1.checkout()
args = self.clone1, 'index.md', 'hello/world.md', branch1.commit.hexsha, 'master'
repo_functions.move_existing_file(*args)
#
# See if the new file made it to clone 2
#
branch2 = repo_functions.start_branch(self.clone2, 'master', name)
branch2.checkout()
self.assertTrue(exists(join(self.clone2.working_dir, 'hello/world.md')))
self.assertFalse(exists(join(self.clone2.working_dir, 'index.md')))
def test_content_merge(self):
''' Test that non-conflicting changes on the same file merge cleanly.
'''
branch1 = repo_functions.start_branch(self.clone1, 'master', 'title')
branch2 = repo_functions.start_branch(self.clone2, 'master', 'body')
branch1.checkout()
branch2.checkout()
with open(self.clone1.working_dir + '/index.md') as file:
front1, _ = jekyll_functions.load_jekyll_doc(file)
with open(self.clone2.working_dir + '/index.md') as file:
_, body2 = jekyll_functions.load_jekyll_doc(file)
#
# Show that only the title branch title is now present on master.
#
repo_functions.complete_branch(self.clone1, 'master', 'title')
with open(self.clone1.working_dir + '/index.md') as file:
front1b, body1b = jekyll_functions.load_jekyll_doc(file)
self.assertEqual(front1b['title'], front1['title'])
self.assertNotEqual(body1b, body2)
#
# Show that the body branch body is also now present on master.
#
repo_functions.complete_branch(self.clone2, 'master', 'body')
with open(self.clone2.working_dir + '/index.md') as file:
front2b, body2b = jekyll_functions.load_jekyll_doc(file)
self.assertEqual(front2b['title'], front1['title'])
self.assertEqual(body2b, body2)
self.assertTrue(self.clone2.commit().message.startswith('Merged work from'))
def test_content_merge_extra_change(self):
''' Test that non-conflicting changes on the same file merge cleanly.
'''
branch1 = repo_functions.start_branch(self.clone1, 'master', 'title')
branch2 = repo_functions.start_branch(self.clone2, 'master', 'body')
branch1.checkout()
branch2.checkout()
with open(self.clone1.working_dir + '/index.md') as file:
front1, _ = jekyll_functions.load_jekyll_doc(file)
with open(self.clone2.working_dir + '/index.md') as file:
front2, body2 = jekyll_functions.load_jekyll_doc(file)
#
# Show that only the title branch title is now present on master.
#
repo_functions.complete_branch(self.clone1, 'master', 'title')
with open(self.clone1.working_dir + '/index.md') as file:
front1b, body1b = jekyll_functions.load_jekyll_doc(file)
self.assertEqual(front1b['title'], front1['title'])
self.assertNotEqual(body1b, body2)
#
# Show that the body branch body is also now present on master.
#
edit_functions.update_page(self.clone2, 'index.md',
front2, 'Another change to the body')
repo_functions.save_working_file(self.clone2, 'index.md', 'A new change',
self.clone2.commit().hexsha, 'master')
#
# Show that upstream changes from master have been merged here.
#
with open(self.clone2.working_dir + '/index.md') as file:
front2b, body2b = jekyll_functions.load_jekyll_doc(file)
self.assertEqual(front2b['title'], front1['title'])
self.assertEqual(body2b.strip(), 'Another change to the body')
self.assertTrue(self.clone2.commit().message.startswith('Merged work from'))
def test_multifile_merge(self):
''' Test that two non-conflicting new files merge cleanly.
'''
name = str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', name)
branch2 = repo_functions.start_branch(self.clone2, 'master', name)
#
# Make new files in each branch and save them.
#
branch1.checkout()
branch2.checkout()
edit_functions.create_new_page(self.clone1, '', 'file1.md',
dict(title='Hello'), 'Hello hello.')
edit_functions.create_new_page(self.clone2, '', 'file2.md',
dict(title='Goodbye'), 'Goodbye goodbye.')
#
# Show that the changes from the first branch made it to origin.
#
args1 = self.clone1, 'file1.md', '...', branch1.commit.hexsha, 'master'
commit1 = repo_functions.save_working_file(*args1)
self.assertEquals(self.origin.branches[name].commit, commit1)
self.assertEquals(self.origin.branches[name].commit.author.email, self.session['email'])
self.assertEquals(self.origin.branches[name].commit.committer.email, self.session['email'])
self.assertEquals(commit1, branch1.commit)
#
# Show that the changes from the second branch also made it to origin.
#
args2 = self.clone2, 'file2.md', '...', branch2.commit.hexsha, 'master'
commit2 = repo_functions.save_working_file(*args2)
self.assertEquals(self.origin.branches[name].commit, commit2)
self.assertEquals(self.origin.branches[name].commit.author.email, self.session['email'])
self.assertEquals(self.origin.branches[name].commit.committer.email, self.session['email'])
self.assertEquals(commit2, branch2.commit)
#
# Show that the merge from the second branch made it back to the first.
#
branch1b = repo_functions.start_branch(self.clone1, 'master', name)
self.assertEquals(branch1b.commit, branch2.commit)
self.assertEquals(branch1b.commit.author.email, self.session['email'])
self.assertEquals(branch1b.commit.committer.email, self.session['email'])
def test_same_branch_conflict(self):
''' Test that a conflict in two branches appears at the right spot.
'''
name = str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', name)
branch2 = repo_functions.start_branch(self.clone2, 'master', name)
#
# Make new files in each branch and save them.
#
branch1.checkout()
branch2.checkout()
edit_functions.create_new_page(self.clone1, '', 'conflict.md',
dict(title='Hello'), 'Hello hello.')
edit_functions.create_new_page(self.clone2, '', 'conflict.md',
dict(title='Goodbye'), 'Goodbye goodbye.')
#
# Show that the changes from the first branch made it to origin.
#
args1 = self.clone1, 'conflict.md', '...', branch1.commit.hexsha, 'master'
commit1 = repo_functions.save_working_file(*args1)
self.assertEquals(self.origin.branches[name].commit, commit1)
self.assertEquals(commit1, branch1.commit)
#
# Show that the changes from the second branch conflict with the first.
#
with self.assertRaises(repo_functions.MergeConflict) as conflict:
args2 = self.clone2, 'conflict.md', '...', branch2.commit.hexsha, 'master'
commit2 = repo_functions.save_working_file(*args2)
self.assertEqual(conflict.exception.remote_commit, commit1)
diffs = conflict.exception.remote_commit.diff(conflict.exception.local_commit)
self.assertEqual(len(diffs), 1)
self.assertEqual(diffs[0].a_blob.name, 'conflict.md')
self.assertEqual(diffs[0].b_blob.name, 'conflict.md')
def test_upstream_pull_conflict(self):
''' Test that a conflict in two branches appears at the right spot.
'''
name1, name2 = str(uuid4()), str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', name1)
branch2 = repo_functions.start_branch(self.clone2, 'master', name2)
#
# Make new files in each branch and save them.
#
branch1.checkout()
branch2.checkout()
edit_functions.create_new_page(self.clone1, '', 'conflict.md',
dict(title='Hello'), 'Hello hello.')
edit_functions.create_new_page(self.clone2, '', 'conflict.md',
dict(title='Goodbye'), 'Goodbye goodbye.')
#
# Show that the changes from the first branch made it to origin.
#
args1 = self.clone1, 'conflict.md', '...', branch1.commit.hexsha, 'master'
commit1 = repo_functions.save_working_file(*args1)
self.assertEquals(self.origin.branches[name1].commit, commit1)
self.assertEquals(commit1, branch1.commit)
#
# Merge the first branch to master.
#
commit2 = repo_functions.complete_branch(self.clone1, 'master', name1)
self.assertFalse(name1 in self.origin.branches)
#
# Show that the changes from the second branch conflict with the first.
#
with self.assertRaises(repo_functions.MergeConflict) as conflict:
args2 = self.clone2, 'conflict.md', '...', branch2.commit.hexsha, 'master'
repo_functions.save_working_file(*args2)
self.assertEqual(conflict.exception.remote_commit, commit2)
diffs = conflict.exception.remote_commit.diff(conflict.exception.local_commit)
self.assertEqual(len(diffs), 1)
self.assertEqual(diffs[0].a_blob.name, 'conflict.md')
self.assertEqual(diffs[0].b_blob.name, 'conflict.md')
def test_upstream_push_conflict(self):
''' Test that a conflict in two branches appears at the right spot.
'''
name1, name2 = str(uuid4()), str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', name1)
branch2 = repo_functions.start_branch(self.clone2, 'master', name2)
#
# Make new files in each branch and save them.
#
branch1.checkout()
branch2.checkout()
edit_functions.create_new_page(self.clone1, '', 'conflict.md',
dict(title='Hello'), 'Hello hello.')
edit_functions.create_new_page(self.clone2, '', 'conflict.md',
dict(title='Goodbye'), 'Goodbye goodbye.')
#
# Push changes from the two branches to origin.
#
args1 = self.clone1, 'conflict.md', '...', branch1.commit.hexsha, 'master'
commit1 = repo_functions.save_working_file(*args1)
args2 = self.clone2, 'conflict.md', '...', branch2.commit.hexsha, 'master'
commit2 = repo_functions.save_working_file(*args2)
#
# Merge the two branches to master; show that second merge will fail.
#
repo_functions.complete_branch(self.clone1, 'master', name1)
self.assertFalse(name1 in self.origin.branches)
with self.assertRaises(repo_functions.MergeConflict) as conflict:
repo_functions.complete_branch(self.clone2, 'master', name2)
self.assertEqual(conflict.exception.remote_commit, self.origin.commit())
self.assertEqual(conflict.exception.local_commit, self.clone2.commit())
diffs = conflict.exception.remote_commit.diff(conflict.exception.local_commit)
self.assertEqual(len(diffs), 1)
self.assertEqual(diffs[0].a_blob.name, 'conflict.md')
self.assertEqual(diffs[0].b_blob.name, 'conflict.md')
def test_conflict_resolution_clobber(self):
''' Test that a conflict in two branches can be clobbered.
'''
name = str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', 'title')
branch2 = repo_functions.start_branch(self.clone2, 'master', name)
#
# Add goner.md in branch1.
#
branch1.checkout()
edit_functions.create_new_page(self.clone1, '', 'goner.md',
dict(title=name), 'Woooo woooo.')
args = self.clone1, 'goner.md', '...', branch1.commit.hexsha, 'master'
commit = repo_functions.save_working_file(*args)
#
# Change index.md in branch2 so it conflicts with title branch.
#
branch2.checkout()
edit_functions.update_page(self.clone2, 'index.md',
dict(title=name), 'Hello hello.')
args = self.clone2, 'index.md', '...', branch2.commit.hexsha, 'master'
commit = repo_functions.save_working_file(*args)
#
# Merge the original title branch, fail to merge our conflicting branch.
#
repo_functions.complete_branch(self.clone1, 'master', 'title')
with self.assertRaises(repo_functions.MergeConflict) as conflict:
repo_functions.complete_branch(self.clone2, 'master', name)
self.assertEqual(conflict.exception.local_commit, commit)
diffs = conflict.exception.remote_commit.diff(conflict.exception.local_commit)
self.assertEqual(len(diffs), 2)
self.assertTrue(diffs[0].a_blob.name in ('index.md', 'goner.md'))
self.assertTrue(diffs[1].a_blob.name in ('index.md', 'goner.md'))
#
# Merge our conflicting branch and clobber the default branch.
#
repo_functions.clobber_default_branch(self.clone2, 'master', name)
with open(join(self.clone2.working_dir, 'index.md')) as file:
front, body = jekyll_functions.load_jekyll_doc(file)
self.assertEqual(front['title'], name)
self.assertFalse(name in self.origin.branches)
# If goner.md is still around, then master wasn't fully clobbered.
self.clone1.branches['master'].checkout()
self.clone1.git.pull('origin', 'master')
self.assertFalse(exists(join(self.clone2.working_dir, 'goner.md')))
self.assertTrue(self.clone2.commit().message.startswith('Clobbered with work from'))
def test_conflict_resolution_abandon(self):
''' Test that a conflict in two branches can be abandoned.
'''
name = str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', 'title')
branch2 = repo_functions.start_branch(self.clone2, 'master', name)
#
# Change index.md in branch2 so it conflicts with title branch.
# Also add goner.md, which we'll later want to disappear.
#
branch2.checkout()
edit_functions.update_page(self.clone2, 'index.md',
dict(title=name), 'Hello hello.')
edit_functions.create_new_page(self.clone2, '', 'goner.md',
dict(title=name), 'Woooo woooo.')
args = self.clone2, 'index.md', '...', branch2.commit.hexsha, 'master'
commit = repo_functions.save_working_file(*args)
args = self.clone2, 'goner.md', '...', branch2.commit.hexsha, 'master'
commit = repo_functions.save_working_file(*args)
#
# Merge the original title branch, fail to merge our conflicting branch.
#
repo_functions.complete_branch(self.clone1, 'master', 'title')
with self.assertRaises(repo_functions.MergeConflict) as conflict:
repo_functions.complete_branch(self.clone2, 'master', name)
self.assertEqual(conflict.exception.local_commit, commit)
diffs = conflict.exception.remote_commit.diff(conflict.exception.local_commit)
self.assertEqual(len(diffs), 2)
self.assertTrue(diffs[0].b_blob.name in ('index.md', 'goner.md'))
self.assertTrue(diffs[1].b_blob.name in ('index.md', 'goner.md'))
#
# Merge our conflicting branch and abandon it to the default branch.
#
repo_functions.abandon_branch(self.clone2, 'master', name)
with open(join(self.clone2.working_dir, 'index.md')) as file:
front, body = jekyll_functions.load_jekyll_doc(file)
self.assertNotEqual(front['title'], name)
self.assertFalse(name in self.origin.branches)
# If goner.md is still around, then the branch wasn't fully abandoned.
self.assertFalse(exists(join(self.clone2.working_dir, 'goner.md')))
self.assertTrue(self.clone2.commit().message.startswith('Abandoned work from'))
def test_peer_review(self):
''' Change the path of a file.
'''
name = str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', name)
#
# Make a commit.
#
environ['GIT_AUTHOR_NAME'] = 'Jim Content Creator'
environ['GIT_COMMITTER_NAME'] = 'Jim Content Creator'
environ['GIT_AUTHOR_EMAIL'] = 'creator@example.com'
environ['GIT_COMMITTER_EMAIL'] = 'creator@example.com'
branch1.checkout()
self.assertFalse(repo_functions.needs_peer_review(self.clone1, 'master', name))
self.assertFalse(repo_functions.is_peer_approved(self.clone1, 'master', name))
edit_functions.update_page(self.clone1, 'index.md',
dict(title=name), 'Hello you-all.')
repo_functions.save_working_file(self.clone1, 'index.md', 'I made a change',
self.clone1.commit().hexsha, 'master')
self.assertTrue(repo_functions.needs_peer_review(self.clone1, 'master', name))
self.assertFalse(repo_functions.is_peer_approved(self.clone1, 'master', name))
self.assertEqual(repo_functions.ineligible_peer(self.clone1, 'master', name), 'creator@example.com')
#
# Approve the work as someone else.
#
environ['GIT_AUTHOR_NAME'] = 'Joe Reviewer'
environ['GIT_COMMITTER_NAME'] = 'Joe Reviewer'
environ['GIT_AUTHOR_EMAIL'] = 'reviewer@example.com'
environ['GIT_COMMITTER_EMAIL'] = 'reviewer@example.com'
repo_functions.mark_as_reviewed(self.clone1)
self.assertFalse(repo_functions.needs_peer_review(self.clone1, 'master', name))
self.assertTrue(repo_functions.is_peer_approved(self.clone1, 'master', name))
self.assertEqual(repo_functions.ineligible_peer(self.clone1, 'master', name), None)
#
# Make another commit.
#
edit_functions.update_page(self.clone1, 'index.md',
dict(title=name), 'Hello you there.')
repo_functions.save_working_file(self.clone1, 'index.md', 'I made a change',
self.clone1.commit().hexsha, 'master')
self.assertTrue(repo_functions.needs_peer_review(self.clone1, 'master', name))
self.assertFalse(repo_functions.is_peer_approved(self.clone1, 'master', name))
self.assertEqual(repo_functions.ineligible_peer(self.clone1, 'master', name), 'reviewer@example.com')
#
# Approve the work as someone else.
#
environ['GIT_AUTHOR_NAME'] = 'Jane Reviewer'
environ['GIT_COMMITTER_NAME'] = 'Jane Reviewer'
environ['GIT_AUTHOR_EMAIL'] = 'reviewer@example.org'
environ['GIT_COMMITTER_EMAIL'] = 'reviewer@example.org'
repo_functions.mark_as_reviewed(self.clone1)
self.assertFalse(repo_functions.needs_peer_review(self.clone1, 'master', name))
self.assertTrue(repo_functions.is_peer_approved(self.clone1, 'master', name))
self.assertEqual(repo_functions.ineligible_peer(self.clone1, 'master', name), None)
def test_peer_rejected(self):
'''
'''
name = str(uuid4())
branch1 = repo_functions.start_branch(self.clone1, 'master', name)
#
# Make a commit.
#
environ['GIT_AUTHOR_NAME'] = 'Jim Content Creator'
environ['GIT_COMMITTER_NAME'] = 'Jim Content Creator'
environ['GIT_AUTHOR_EMAIL'] = 'creator@example.com'
environ['GIT_COMMITTER_EMAIL'] = 'creator@example.com'
branch1.checkout()
self.assertFalse(repo_functions.needs_peer_review(self.clone1, 'master', name))
self.assertFalse(repo_functions.is_peer_approved(self.clone1, 'master', name))
edit_functions.update_page(self.clone1, 'index.md',
dict(title=name), 'Hello you-all.')
repo_functions.save_working_file(self.clone1, 'index.md', 'I made a change',
self.clone1.commit().hexsha, 'master')
self.assertTrue(repo_functions.needs_peer_review(self.clone1, 'master', name))
self.assertFalse(repo_functions.is_peer_approved(self.clone1, 'master', name))
self.assertFalse(repo_functions.is_peer_rejected(self.clone1, 'master', name))
self.assertEqual(repo_functions.ineligible_peer(self.clone1, 'master', name), 'creator@example.com')
#
# Approve the work as someone else.
#
environ['GIT_AUTHOR_NAME'] = 'Joe Reviewer'
environ['GIT_COMMITTER_NAME'] = 'Joe Reviewer'
environ['GIT_AUTHOR_EMAIL'] = 'reviewer@example.com'
environ['GIT_COMMITTER_EMAIL'] = 'reviewer@example.com'
repo_functions.provide_feedback(self.clone1, 'This sucks.')
self.assertFalse(repo_functions.needs_peer_review(self.clone1, 'master', name))
self.assertFalse(repo_functions.is_peer_approved(self.clone1, 'master', name))
self.assertTrue(repo_functions.is_peer_rejected(self.clone1, 'master', name))
self.assertEqual(repo_functions.ineligible_peer(self.clone1, 'master', name), None)
#
# Make another commit.
#
edit_functions.update_page(self.clone1, 'index.md',
dict(title=name), 'Hello you there.')
repo_functions.save_working_file(self.clone1, 'index.md', 'I made a change',
self.clone1.commit().hexsha, 'master')
self.assertTrue(repo_functions.needs_peer_review(self.clone1, 'master', name))
self.assertFalse(repo_functions.is_peer_approved(self.clone1, 'master', name))
self.assertFalse(repo_functions.is_peer_rejected(self.clone1, 'master', name))
self.assertEqual(repo_functions.ineligible_peer(self.clone1, 'master', name), 'reviewer@example.com')
#
# Approve the work as someone else.
#
environ['GIT_AUTHOR_NAME'] = 'Jane Reviewer'
environ['GIT_COMMITTER_NAME'] = 'Jane Reviewer'
environ['GIT_AUTHOR_EMAIL'] = 'reviewer@example.org'
environ['GIT_COMMITTER_EMAIL'] = 'reviewer@example.org'
repo_functions.provide_feedback(self.clone1, 'This still sucks.')
self.assertFalse(repo_functions.needs_peer_review(self.clone1, 'master', name))
self.assertFalse(repo_functions.is_peer_approved(self.clone1, 'master', name))
self.assertTrue(repo_functions.is_peer_rejected(self.clone1, 'master', name))
self.assertEqual(repo_functions.ineligible_peer(self.clone1, 'master', name), None)
#
(email2, message2), (email1, message1) = repo_functions.get_rejection_messages(self.clone1, 'master', name)
self.assertEqual(email1, 'reviewer@example.com')
self.assertTrue('This sucks.' in message1)
self.assertEqual(email2, 'reviewer@example.org')
self.assertTrue('This still sucks.' in message2)
''' Test functions that are called outside of the google authing/analytics data fetching via the UI
'''
class TestGoogleApiFunctions (TestCase):
def setUp(self):
app_args = {}
app_args['GA_CLIENT_ID'] = 'client_id'
app_args['GA_CLIENT_SECRET'] = 'meow_secret'
self.ga_config_dir = mkdtemp(prefix='bizarro-config-')
app_args['RUNNING_STATE_DIR'] = self.ga_config_dir
self.app = create_app(app_args)
# write a tmp config file
config_values = {
"access_token": "meowser_token",
"refresh_token": "refresh_meows",
"profile_id": "12345678",
"project_domain": "example.com"
}
with self.app.app_context():
google_api_functions.write_ga_config(config_values, self.app.config['RUNNING_STATE_DIR'])
def tearDown(self):
rmtree(self.ga_config_dir)
def mock_successful_request_new_google_access_token(self, url, request):
if google_api_functions.GOOGLE_ANALYTICS_TOKENS_URL in url.geturl():
return response(200, '''{"access_token": "meowser_access_token", "token_type": "meowser_type", "expires_in": 3920}''')
else:
raise Exception('01 Asked for unknown URL ' + url.geturl())
def mock_failed_request_new_google_access_token(self, url, request):
if google_api_functions.GOOGLE_ANALYTICS_TOKENS_URL in url.geturl():
return response(500)
else:
raise Exception('02 Asked for unknown URL ' + url.geturl())
def mock_google_analytics_authorized_response(self, url, request):
if 'https://www.googleapis.com/analytics/' in url.geturl():
return response(200, '''{"totalsForAllResults": {"ga:pageViews": "24", "ga:avgTimeOnPage": "67.36363636363636"}}''')
def test_successful_request_new_google_access_token(self):
with self.app.test_request_context():
with HTTMock(self.mock_successful_request_new_google_access_token):
google_api_functions.request_new_google_access_token('meowser_refresh_token', self.app.config['RUNNING_STATE_DIR'], self.app.config['GA_CLIENT_ID'], self.app.config['GA_CLIENT_SECRET'])
with self.app.app_context():
ga_config = google_api_functions.read_ga_config(self.app.config['RUNNING_STATE_DIR'])
self.assertEqual(ga_config['access_token'], 'meowser_access_token')
def test_failure_to_request_new_google_access_token(self):