def test_incorrect_authorization(self):
        """Ensures that when auth is incorrect the right exception is thrown"""

        response_headers = {
            reproduce.CLUSTERFUZZ_AUTH_HEADER: 'Bearer 12345',
            reproduce.CLUSTERFUZZ_AUTH_IDENTITY: 'identity@something'
        }
        response_dict = {
            'status': 401,
            'type': 'UnauthorizedException',
            'message': {
                'Invalid verification code (12345)': {
                    'error': 'invalid_grant',
                    'error_description': 'Bad Request'
                }
            },
            'params': {
                'testcaseId': ['999']
            },
            'email': '*****@*****.**'
        }

        self.mock.get_stored_auth_header.return_value = 'Bearer 12345'
        self.mock.get_verification_header.return_value = 'VerificationCode 12345'
        self.mock.post.return_value = mock.Mock(status_code=401,
                                                text=json.dumps(response_dict),
                                                headers=response_headers)

        with self.assertRaises(error.ClusterFuzzError) as cm:
            reproduce.send_request('url', 'data')

        self.assertEqual(401, cm.exception.status_code)
        self.assert_exact_calls(self.mock.post, [
            mock.call(allow_redirects=True,
                      url='url',
                      data='data',
                      headers={
                          'Authorization': 'Bearer 12345',
                          'User-Agent': 'clusterfuzz-tools'
                      })
        ] + [
            mock.call(allow_redirects=True,
                      url='url',
                      data='data',
                      headers={
                          'Authorization': 'VerificationCode 12345',
                          'User-Agent': 'clusterfuzz-tools'
                      })
        ] * (reproduce.RETRY_COUNT - 1))
    def test_correct_verification_auth(self):
        """Tests grabbing testcase info when the local header is invalid."""

        response_headers = {reproduce.CLUSTERFUZZ_AUTH_HEADER: 'Bearer 12345'}
        response_dict = {
            'id': '12345',
            'crash_type': 'Bad Crash',
            'crash_state': ['Halted']
        }

        self.mock.get_stored_auth_header.return_value = None
        self.mock.get_verification_header.return_value = 'VerificationCode 12345'
        self.mock.post.return_value = mock.Mock(status_code=200,
                                                text=json.dumps(response_dict),
                                                headers=response_headers)

        response = reproduce.send_request('url', 'data')

        self.assert_exact_calls(self.mock.get_stored_auth_header,
                                [mock.call()])
        self.assert_exact_calls(self.mock.get_verification_header,
                                [mock.call()])
        self.assert_exact_calls(self.mock.store_auth_header,
                                [mock.call('Bearer 12345')])
        self.assert_exact_calls(self.mock.post, [
            mock.call(allow_redirects=True,
                      data='data',
                      url='url',
                      headers={
                          'Authorization': 'VerificationCode 12345',
                          'User-Agent': 'clusterfuzz-tools'
                      })
        ])
        self.assertEqual(200, response.status_code)
示例#3
0
    def test_correct_stored_authorization(self):
        """Ensures that the testcase info is returned when stored auth is correct"""

        response_headers = {'x-clusterfuzz-authorization': 'Bearer 12345'}
        response_dict = {
            'id': '12345',
            'crash_type': 'Bad Crash',
            'crash_state': ['Halted']
        }

        self.mock.get_stored_auth_header.return_value = 'Bearer 12345'
        self.mock.post.return_value = mock.Mock(status_code=200,
                                                text=json.dumps(response_dict),
                                                headers=response_headers)

        response = reproduce.send_request('url', 'data')

        self.assert_exact_calls(self.mock.get_stored_auth_header,
                                [mock.call()])
        self.assert_exact_calls(self.mock.store_auth_header,
                                [mock.call('Bearer 12345')])
        self.assert_exact_calls(self.mock.post, [
            mock.call(url='url',
                      headers={
                          'Authorization': 'Bearer 12345',
                          'User-Agent': 'clusterfuzz-tools'
                      },
                      data='data',
                      allow_redirects=True)
        ])
        self.assertEqual(200, response.status_code)
示例#4
0
    def test_incorrect_stored_header(self):
        """Tests when the header is stored, but has expired/is invalid."""

        response_headers = {'x-clusterfuzz-authorization': 'Bearer 12345'}
        response_dict = {
            'id': '12345',
            'crash_type': 'Bad Crash',
            'crash_state': ['Halted']
        }

        self.mock.post.side_effect = [
            mock.Mock(status_code=401),
            mock.Mock(status_code=200,
                      text=json.dumps(response_dict),
                      headers=response_headers)
        ]
        self.mock.get_stored_auth_header.return_value = 'Bearer 12345'
        self.mock.get_verification_header.return_value = 'VerificationCode 12345'

        response = reproduce.send_request('url', 'data')

        self.assert_exact_calls(self.mock.get_stored_auth_header,
                                [mock.call()])
        self.assert_exact_calls(self.mock.get_verification_header,
                                [mock.call()])
        self.assert_exact_calls(self.mock.post, [
            mock.call(allow_redirects=True,
                      url='url',
                      data='data',
                      headers={
                          'Authorization': 'Bearer 12345',
                          'User-Agent': 'clusterfuzz-tools'
                      }),
            mock.call(headers={
                'Authorization': 'VerificationCode 12345',
                'User-Agent': 'clusterfuzz-tools'
            },
                      allow_redirects=True,
                      data='data',
                      url='url')
        ])
        self.assert_exact_calls(self.mock.store_auth_header,
                                [mock.call('Bearer 12345')])
        self.assertEqual(200, response.status_code)
    def test_correct_stored_authorization(self):
        """Ensures that the testcase info is returned when stored auth is correct"""

        response_headers = {
            reproduce.CLUSTERFUZZ_AUTH_HEADER: 'Bearer 12345',
            reproduce.CLUSTERFUZZ_AUTH_IDENTITY: 'identity@something'
        }
        response_dict = {
            'id': '12345',
            'crash_type': 'Bad Crash',
            'crash_state': ['Halted']
        }

        self.mock.get_stored_auth_header.return_value = 'Bearer 12345'
        self.mock.post.side_effect = [
            mock.Mock(status_code=500, text='', headers=''),
            mock.Mock(status_code=500, text='', headers=''),
            mock.Mock(status_code=200,
                      text=json.dumps(response_dict),
                      headers=response_headers),
        ]

        response = reproduce.send_request('url', 'data')

        self.assert_exact_calls(self.mock.get_stored_auth_header,
                                [mock.call()])
        self.assert_exact_calls(self.mock.store_auth_header,
                                [mock.call('Bearer 12345')])
        self.assert_exact_calls(self.mock.post, [
            mock.call(url='url',
                      data='data',
                      allow_redirects=True,
                      headers={
                          'Authorization': 'Bearer 12345',
                          'User-Agent': 'clusterfuzz-tools'
                      })
        ] * 3)
        self.assertEqual(200, response.status_code)
        self.assertEqual('identity@something',
                         response.headers[reproduce.CLUSTERFUZZ_AUTH_IDENTITY])