def test_non_json_return_type(self, mock):
     '''Test for invalid JSON return type'''
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         text='non-json',
         status_code=httplib.OK
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     self.assertRaises(
         exceptions.UnexpectedResponse,
         client.request_access_token)
Exemplo n.º 2
0
 def test_non_json_return_type(self, mock):
     '''Test for invalid JSON return type'''
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         text='non-json',
         status_code=httplib.OK
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     self.assertRaises(
         exceptions.UnexpectedResponse,
         client.request_access_token)
 def test_missing_token(self, mock):
     '''Test for missing token response'''
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         json={},
         status_code=httplib.OK
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     self.assertRaises(
         exceptions.UnexpectedResponse,
         client.request_access_token)
 def test_good_token(self, mock):
     '''Test for good token response'''
     token = '1234-1234-1234-1234'
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         json={'access_token': token},
         status_code=httplib.OK
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     data = client.request_access_token()
     self.assertEqual(data.get('access_token'), token)
Exemplo n.º 5
0
 def test_good_token(self, mock):
     '''Test for good token response'''
     token = '1234-1234-1234-1234'
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         json={'access_token': token},
         status_code=httplib.OK
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     data = client.request_access_token()
     self.assertEqual(data.get('access_token'), token)
 def test_unexpected_http_code(self, mock):
     '''Test for unexpected HTTP status code'''
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         json=None,
         status_code=httplib.SERVICE_UNAVAILABLE
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     self.assertRaises(
         exceptions.UnexpectedResponse,
         client.request_access_token)
Exemplo n.º 7
0
 def test_missing_token(self, mock):
     '''Test for missing token response'''
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         json={},
         status_code=httplib.OK
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     self.assertRaises(
         exceptions.UnexpectedResponse,
         client.request_access_token)
Exemplo n.º 8
0
 def test_unexpected_http_code(self, mock):
     '''Test for unexpected HTTP status code'''
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         json=None,
         status_code=httplib.SERVICE_UNAVAILABLE
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     self.assertRaises(
         exceptions.UnexpectedResponse,
         client.request_access_token)
Exemplo n.º 9
0
    def test_upload(self):
        self.session.add(ApiKey(valid_key='e5444-794', log_submit=True))
        self.session.flush()

        reports = []
        reports.extend(
            self.add_reports(1,
                             email='secretemail@localhost',
                             ip=self.geoip_data['London']['ip']))
        reports.extend(self.add_reports(1, api_key='e5444e9f-7946'))
        reports.extend(self.add_reports(1, api_key=None))

        with requests_mock.Mocker() as mock:
            mock.register_uri('POST', requests_mock.ANY, text='{}')
            schedule_export_reports.delay().get()

        self.assertEqual(mock.call_count, 1)
        req = mock.request_history[0]

        # check headers
        self.assertEqual(req.headers['Content-Type'], 'application/json')
        self.assertEqual(req.headers['Content-Encoding'], 'gzip')
        self.assertEqual(req.headers['User-Agent'], 'ichnaea')

        # check body
        body = util.decode_gzip(req.body)
        # make sure we don't accidentally leak emails or IPs
        self.assertFalse('secretemail' in body)
        self.assertFalse(self.geoip_data['London']['ip'] in body)

        # make sure a standards based json can decode this data
        # and none of our internal_json structures end up in it
        send_reports = json.loads(body)['items']
        self.assertEqual(len(send_reports), 3)
        expect = [report['position']['accuracy'] for report in reports]
        gotten = [report['position']['accuracy'] for report in send_reports]
        self.assertEqual(set(expect), set(gotten))

        self.assertEqual(
            set([w['ssid'] for w in send_reports[0]['wifiAccessPoints']]),
            set(['my-wifi']))

        self.check_stats(counter=[
            ('data.export.batch', 1, 1, ['key:test']),
            ('data.export.upload', 1, ['key:test', 'status:200']),
        ],
                         timer=[
                             ('data.export.upload', ['key:test']),
                         ])
 def test_unauthorized(self, mock):
     '''Test for general "unauthorized" return'''
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         json={
             'error': 'unknown_code'
         },
         status_code=httplib.UNAUTHORIZED
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     self.assertRaises(
         exceptions.UnauthorizedRequest,
         client.request_access_token)
 def test_bad_credentials(self, mock):
     '''Test for invalid credentials return'''
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         json={
             'error': 'invalid_client'
         },
         status_code=httplib.UNAUTHORIZED
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     self.assertRaises(
         exceptions.InvalidCredentials,
         client.request_access_token)
Exemplo n.º 12
0
 def test_bad_credentials(self, mock):
     '''Test for invalid credentials return'''
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         json={
             'error': 'invalid_client'
         },
         status_code=httplib.UNAUTHORIZED
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     self.assertRaises(
         exceptions.InvalidCredentials,
         client.request_access_token)
Exemplo n.º 13
0
 def test_unauthorized(self, mock):
     '''Test for general "unauthorized" return'''
     mock.register_uri(
         'POST',
         '{0}/{1}/oauth2/token'.format(
             constants.OAUTH2_ENDPOINT,
             self.credentials.tenant_id),
         json={
             'error': 'unknown_code'
         },
         status_code=httplib.UNAUTHORIZED
     )
     client = oauth2.OAuth2(self.credentials, logger=self.log)
     self.assertRaises(
         exceptions.UnauthorizedRequest,
         client.request_access_token)
Exemplo n.º 14
0
    def test_upload(self, celery, session, stats):
        ApiKeyFactory(valid_key='e5444-794')
        ExportConfigFactory(
            name='test', batch=3, schema='geosubmit',
            url='http://127.0.0.1:9/v2/geosubmit?key=external')
        session.flush()

        reports = []
        reports.extend(self.add_reports(
            celery, 1, source='gnss'))
        reports.extend(self.add_reports(
            celery, 1, api_key='e5444e9f-7946'))
        reports.extend(self.add_reports(
            celery, 1, api_key=None, source='fused'))

        with requests_mock.Mocker() as mock:
            mock.register_uri('POST', requests_mock.ANY, text='{}')
            update_incoming.delay().get()

        assert mock.call_count == 1
        req = mock.request_history[0]

        # check headers
        assert req.headers['Content-Type'] == 'application/json'
        assert req.headers['Content-Encoding'] == 'gzip'
        assert req.headers['User-Agent'] == 'ichnaea'

        body = util.decode_gzip(req.body)
        send_reports = simplejson.loads(body)['items']
        assert len(send_reports) == 3

        for field in ('accuracy', 'source', 'timestamp'):
            expect = [report['position'].get(field) for report in reports]
            gotten = [report['position'].get(field) for report in send_reports]
            assert set(expect) == set(gotten)

        assert (
            set([w['ssid'] for w in send_reports[0]['wifiAccessPoints']]) ==
            set(['my-wifi']))

        stats.check(counter=[
            ('data.export.batch', 1, 1, ['key:test']),
            ('data.export.upload', 1, ['key:test', 'status:200']),
        ], timer=[
            ('data.export.upload', ['key:test']),
        ])
Exemplo n.º 15
0
    def test_upload(self):
        self.session.add(ApiKey(valid_key='e5444-794', log_submit=True))
        self.session.flush()

        reports = []
        reports.extend(self.add_reports(1, email='secretemail@localhost',
                                        ip=self.geoip_data['London']['ip']))
        reports.extend(self.add_reports(1, api_key='e5444e9f-7946'))
        reports.extend(self.add_reports(1, api_key=None))

        with requests_mock.Mocker() as mock:
            mock.register_uri('POST', requests_mock.ANY, text='{}')
            schedule_export_reports.delay().get()

        self.assertEqual(mock.call_count, 1)
        req = mock.request_history[0]

        # check headers
        self.assertEqual(req.headers['Content-Type'], 'application/json')
        self.assertEqual(req.headers['Content-Encoding'], 'gzip')
        self.assertEqual(req.headers['User-Agent'], 'ichnaea')

        # check body
        body = util.decode_gzip(req.body)
        # make sure we don't accidentally leak emails or IPs
        self.assertFalse('secretemail' in body)
        self.assertFalse(self.geoip_data['London']['ip'] in body)

        # make sure a standards based json can decode this data
        # and none of our internal_json structures end up in it
        send_reports = json.loads(body)['items']
        self.assertEqual(len(send_reports), 3)
        expect = [report['position']['accuracy'] for report in reports]
        gotten = [report['position']['accuracy'] for report in send_reports]
        self.assertEqual(set(expect), set(gotten))

        self.assertEqual(
            set([w['ssid'] for w in send_reports[0]['wifiAccessPoints']]),
            set(['my-wifi']))

        self.check_stats(counter=[
            ('data.export.batch', 1, 1, ['key:test']),
            ('data.export.upload', 1, ['key:test', 'status:200']),
        ], timer=[
            ('data.export.upload', ['key:test']),
        ])
Exemplo n.º 16
0
    def test_upload(self):
        ApiKeyFactory(valid_key='e5444-794')
        self.session.flush()

        reports = []
        reports.extend(self.add_reports(1))
        reports.extend(self.add_reports(1, api_key='e5444e9f-7946'))
        reports.extend(self.add_reports(1, api_key=None))

        with requests_mock.Mocker() as mock:
            mock.register_uri('POST', requests_mock.ANY, text='{}')
            update_incoming.delay().get()

        self.assertEqual(mock.call_count, 1)
        req = mock.request_history[0]

        # check headers
        self.assertEqual(req.headers['Content-Type'], 'application/json')
        self.assertEqual(req.headers['Content-Encoding'], 'gzip')
        self.assertEqual(req.headers['User-Agent'], 'ichnaea')

        body = util.decode_gzip(req.body)
        send_reports = simplejson.loads(body)['items']
        self.assertEqual(len(send_reports), 3)
        expect = [report['position']['accuracy'] for report in reports]
        gotten = [report['position']['accuracy'] for report in send_reports]
        self.assertEqual(set(expect), set(gotten))

        self.assertEqual(
            set([w['ssid'] for w in send_reports[0]['wifiAccessPoints']]),
            set(['my-wifi']))

        self.check_stats(counter=[
            ('data.export.batch', 1, 1, ['key:test']),
            ('data.export.upload', 1, ['key:test', 'status:200']),
        ], timer=[
            ('data.export.upload', ['key:test']),
        ])
Exemplo n.º 17
0
    def test_upload(self):
        ApiKeyFactory(valid_key='e5444-794')
        self.session.flush()

        reports = []
        reports.extend(self.add_reports(1))
        reports.extend(self.add_reports(1, api_key='e5444e9f-7946'))
        reports.extend(self.add_reports(1, api_key=None))

        with requests_mock.Mocker() as mock:
            mock.register_uri('POST', requests_mock.ANY, text='{}')
            schedule_export_reports.delay().get()

        self.assertEqual(mock.call_count, 1)
        req = mock.request_history[0]

        # check headers
        self.assertEqual(req.headers['Content-Type'], 'application/json')
        self.assertEqual(req.headers['Content-Encoding'], 'gzip')
        self.assertEqual(req.headers['User-Agent'], 'ichnaea')

        body = util.decode_gzip(req.body)
        send_reports = simplejson.loads(body)['items']
        self.assertEqual(len(send_reports), 3)
        expect = [report['position']['accuracy'] for report in reports]
        gotten = [report['position']['accuracy'] for report in send_reports]
        self.assertEqual(set(expect), set(gotten))

        self.assertEqual(
            set([w['ssid'] for w in send_reports[0]['wifiAccessPoints']]),
            set(['my-wifi']))

        self.check_stats(counter=[
            ('data.export.batch', 1, 1, ['key:test']),
            ('data.export.upload', 1, ['key:test', 'status:200']),
        ], timer=[
            ('data.export.upload', ['key:test']),
        ])
Exemplo n.º 18
0
    def _populate_mock(self,
                       mock,
                       test_file,
                       status,
                       content_type,
                       etag=None,
                       headers=None,
                       url=BASE_URL,
                       is_cloudflare=False):

        content = open(os.path.join(TEST_FILES_FOLDER, test_file), "rb").read()

        ret_headers = {"Content-Type": content_type, "etag": "an-etag"}
        if headers is not None:
            ret_headers = {**ret_headers, **headers}

        {"Content-Type": content_type, "etag": "an-etag"}

        if is_cloudflare:
            agent = "{user_agent} (+{server}; Updater; {subs} subscribers)".format(
                user_agent=settings.FEEDS_USER_AGENT,
                server=settings.FEEDS_SERVER,
                subs=1)

            mock.register_uri('GET',
                              url,
                              request_headers={"User-Agent": agent},
                              status_code=status,
                              content=content,
                              headers=ret_headers)
        else:
            if etag is None:
                mock.register_uri('GET',
                                  url,
                                  status_code=status,
                                  content=content,
                                  headers=ret_headers)
            else:
                mock.register_uri('GET',
                                  url,
                                  request_headers={'If-None-Match': etag},
                                  status_code=status,
                                  content=content,
                                  headers=ret_headers)