|
1 | 1 | importpytest
|
2 | 2 |
|
3 | 3 | importrequests
|
| 4 | + |
4 | 5 | fromgithub3importsession
|
| 6 | +frommockimportpatch,Mock |
5 | 7 |
|
6 | 8 |
|
7 | 9 | classTestGitHubSession:
|
@@ -70,6 +72,49 @@ def test_basic_login(self):
|
70 | 72 | s.basic_auth('username','password')
|
71 | 73 | asserts.auth== ('username','password')
|
72 | 74 |
|
| 75 | +@patch.object(requests.Session,'request') |
| 76 | +deftest_handle_two_factor_auth(self,request_mock): |
| 77 | +"""Test the method that handles getting the 2fa code""" |
| 78 | +s=self.build_session() |
| 79 | +s.two_factor_auth_callback(lambda:'fake') |
| 80 | +args= ('GET','http://example.com') |
| 81 | +s.handle_two_factor_auth(args, {}) |
| 82 | +request_mock.assert_called_once_with( |
| 83 | +*args, |
| 84 | +headers={'X-GitHub-OTP':'fake'} |
| 85 | + ) |
| 86 | + |
| 87 | +@patch.object(requests.Session,'request') |
| 88 | +deftest_request_ignores_responses_that_do_not_require_2fa(self, |
| 89 | +request_mock): |
| 90 | +"""Test that request does not try to handle 2fa when it should not""" |
| 91 | +response=Mock() |
| 92 | +response.configure_mock(status_code=200,headers={}) |
| 93 | +request_mock.return_value=response |
| 94 | +s=self.build_session() |
| 95 | +s.two_factor_auth_callback(lambda:'fake') |
| 96 | +r=s.get('http://example.com') |
| 97 | +assertrisresponse |
| 98 | +request_mock.assert_called_once_with( |
| 99 | +'GET','http://example.com',allow_redirects=True |
| 100 | + ) |
| 101 | + |
| 102 | +@patch.object(requests.Session,'request') |
| 103 | +deftest_creates_history_while_handling_2fa(self,request_mock): |
| 104 | +"""Test that the overridden request method will create history""" |
| 105 | +response=Mock() |
| 106 | +response.configure_mock( |
| 107 | +status_code=401, |
| 108 | +headers={'X-GitHub-OTP':'required;2fa'}, |
| 109 | +history=[] |
| 110 | + ) |
| 111 | +request_mock.return_value=response |
| 112 | +s=self.build_session() |
| 113 | +s.two_factor_auth_callback(lambda:'fake') |
| 114 | +r=s.get('http://example.com') |
| 115 | +assertlen(r.history)!=0 |
| 116 | +assertrequest_mock.call_count==2 |
| 117 | + |
73 | 118 | deftest_token_auth(self):
|
74 | 119 | """Test that token auth will work with a valid token"""
|
75 | 120 | s=self.build_session()
|
|