Cleanup tests
Jonas Haag
9 years ago
54 | 54 | with testserver(): |
55 | 55 | assert can_reach_site_unauthorized() |
56 | 56 | assert can_reach_site_authorized() |
57 | assert_cannot_clone() | |
57 | ||
58 | assert not can_clone_unauthorized() | |
59 | assert not can_clone_authorized() | |
60 | ||
58 | 61 | assert not can_push_unauthorized() |
59 | 62 | assert not can_push_authorized() |
60 | 63 | |
66 | 69 | testserver(use_smarthttp=True, htdigest_file=open(HTDIGEST_FILE), disable_push=True) |
67 | 70 | ]: |
68 | 71 | with server: |
72 | assert can_reach_site_unauthorized() | |
73 | assert can_reach_site_authorized() | |
74 | ||
69 | 75 | assert can_clone_unauthorized() |
76 | assert can_clone_authorized() | |
77 | ||
70 | 78 | assert not can_push_unauthorized() |
71 | 79 | assert not can_push_authorized() |
72 | 80 | |
73 | 81 | |
74 | 82 | def test_smarthttp_push_no_require_browser_auth(): |
75 | 83 | with testserver(use_smarthttp=True, htdigest_file=open(HTDIGEST_FILE)): |
84 | assert can_reach_site_unauthorized() | |
85 | assert can_reach_site_authorized() | |
86 | ||
76 | 87 | assert can_clone_unauthorized() |
88 | assert can_clone_authorized() | |
89 | ||
77 | 90 | assert not can_push_unauthorized() |
78 | 91 | assert can_push_authorized() |
79 | 92 | |
82 | 95 | with testserver_require_auth(): |
83 | 96 | assert not can_reach_site_unauthorized() |
84 | 97 | assert can_reach_site_authorized() |
98 | ||
85 | 99 | assert not can_clone_unauthorized() |
86 | 100 | assert not can_clone_authorized() |
101 | ||
87 | 102 | assert not can_push_unauthorized() |
88 | 103 | assert not can_push_authorized() |
89 | 104 | |
90 | 105 | |
91 | 106 | def test_smarthttp_require_browser_auth(): |
92 | 107 | with testserver_require_auth(use_smarthttp=True, disable_push=True): |
108 | assert not can_reach_site_unauthorized() | |
109 | assert can_reach_site_authorized() | |
110 | ||
93 | 111 | assert not can_clone_unauthorized() |
94 | 112 | assert can_clone_authorized() |
113 | ||
95 | 114 | assert not can_push_unauthorized() |
96 | 115 | assert not can_push_authorized() |
97 | 116 | |
98 | 117 | |
99 | 118 | def test_smarthttp_push_require_browser_auth(): |
100 | 119 | with testserver_require_auth(use_smarthttp=True): |
120 | assert not can_reach_site_unauthorized() | |
121 | assert can_reach_site_authorized() | |
122 | ||
101 | 123 | assert not can_clone_unauthorized() |
102 | 124 | assert can_clone_authorized() |
125 | ||
103 | 126 | assert not can_push_unauthorized() |
104 | 127 | assert can_push_authorized() |
105 | ||
106 | ||
107 | def assert_cannot_clone(): | |
108 | assert "git clone" not in GET_unauthorized(TEST_REPO_URL).content | |
109 | assert 404 == GET_unauthorized(TEST_REPO_URL + "info/refs?service=git-upload-pack").status_code | |
110 | assert 128 == subprocess.call(["git", "clone", AUTHORIZED_TEST_REPO_URL]) | |
111 | ||
112 | ||
113 | def assert_cannot_push_authorized(expected_status): | |
114 | assert expected_status == GET_authorized(TEST_REPO_URL + "info/refs?service=git-receive-pack").status_code | |
115 | assert expected_status == GET_authorized(TEST_REPO_URL + "git-receive-pack").status_code | |
116 | assert 128 == subprocess.call(["git", "push", AUTHORIZED_TEST_REPO_URL, "master"], cwd=TEST_REPO) | |
117 | ||
118 | ||
119 | def assert_cannot_push_unauthorized(expected_status): | |
120 | assert expected_status == GET_unauthorized(TEST_REPO_URL + "info/refs?service=git-receive-pack").status_code | |
121 | assert expected_status == GET_unauthorized(TEST_REPO_URL + "git-receive-pack").status_code | |
122 | # XXX 'git push' asks for a new username/password and blocks | |
123 | # assert 128 == subprocess.call(["git", "push", UNAUTHORIZED_TEST_REPO_URL, "master"], cwd=TEST_REPO) | |
124 | ||
125 | ||
126 | # Clone | |
127 | def can_clone_unauthorized(): | |
128 | tmp = tempfile.mkdtemp() | |
129 | try: | |
130 | return subprocess.call(["git", "clone", UNAUTHORIZED_TEST_REPO_URL, tmp]) == 0 | |
131 | finally: | |
132 | shutil.rmtree(tmp, ignore_errors=True) | |
133 | ||
134 | def can_clone_authorized(): | |
135 | tmp = tempfile.mkdtemp() | |
136 | try: | |
137 | return subprocess.call(["git", "clone", AUTHORIZED_TEST_REPO_URL, tmp]) == 0 | |
138 | finally: | |
139 | shutil.rmtree(tmp, ignore_errors=True) | |
140 | ||
141 | ||
142 | # Push | |
143 | def can_push_unauthorized(): | |
144 | return subprocess.call(["git", "push", UNAUTHORIZED_TEST_REPO_URL, "master"], cwd=TEST_REPO) == 0 | |
145 | ||
146 | def can_push_authorized(): | |
147 | return subprocess.call(["git", "push", AUTHORIZED_TEST_REPO_URL, "master"], cwd=TEST_REPO) == 0 | |
148 | 128 | |
149 | 129 | |
150 | 130 | # Reach |
153 | 133 | |
154 | 134 | def can_reach_site_authorized(): |
155 | 135 | return GET_authorized(TEST_REPO_URL).status_code == 200 |
136 | ||
137 | ||
138 | # Clone | |
139 | def can_clone_unauthorized(): | |
140 | return _can_clone(GET_unauthorized, UNAUTHORIZED_TEST_REPO_URL) | |
141 | ||
142 | def can_clone_authorized(): | |
143 | return _can_clone(GET_authorized, AUTHORIZED_TEST_REPO_URL) | |
144 | ||
145 | def _can_clone(get, url): | |
146 | tmp = tempfile.mkdtemp() | |
147 | try: | |
148 | return any([ | |
149 | "git clone" in get(TEST_REPO_URL).content, | |
150 | get(TEST_REPO_URL + "info/refs?service=git-upload-pack").status_code == 200, | |
151 | subprocess.call(["git", "clone", url, tmp]) == 0, | |
152 | ]) | |
153 | finally: | |
154 | shutil.rmtree(tmp, ignore_errors=True) | |
155 | ||
156 | ||
157 | # Push | |
158 | def can_push_unauthorized(): | |
159 | return _can_push(GET_unauthorized, UNAUTHORIZED_TEST_REPO_URL) | |
160 | ||
161 | def can_push_authorized(): | |
162 | return _can_push(GET_authorized, AUTHORIZED_TEST_REPO_URL) | |
163 | ||
164 | def _can_push(get, url): | |
165 | return any([ | |
166 | get(TEST_REPO_URL + "info/refs?service=git-receive-pack").status_code == 200, | |
167 | get(TEST_REPO_URL + "git-receive-pack").status_code == 200, | |
168 | subprocess.call(["git", "push", url, "master"], cwd=TEST_REPO) == 0, | |
169 | ]) |