Exemplo n.º 1
0
 def test_refresh_token_error_not_2xx(self):
     with requests_mock.Mocker(real_http=True) as mock:
         mock.register_uri("GET",
                           self.client.get_url("identity/oauth/token"),
                           status_code=404)
         with self.assertRaises(ApiException):
             self.client.refresh_token()
Exemplo n.º 2
0
 def test_refresh_token_error_raises_exception(self):
     with requests_mock.Mocker(real_http=True) as mock:
         mock.register_uri("GET",
                           self.client.get_url("identity/oauth/token"),
                           json={"error": "oops"})
         with self.assertRaises(ApiException):
             self.client.refresh_token()
Exemplo n.º 3
0
 def test_get_apps_401(self, mock):
     mock.register_uri('GET', "http://*****:*****@some-marathon.com/v2/apps", text="some html",
                       status_code=401)
     self.assertEquals([], update.get_apps(self.marathon))
     self.assertEqual(None, config.rdb.get(self.marathon['host'] + '-cache'))
     self.assertEquals(b'could not read marathon: some-marathon.com [ JSONDecodeError ]',
                       config.rdb.get(self.marathon['host'] + '-errors'))
Exemplo n.º 4
0
    def test_update_calls_today(self):
        self.client.token_expires = pendulum.utcnow().add(days=1)
        with requests_mock.Mocker(real_http=True) as mock:
            mock.register_uri("GET",
                              self.client.get_url("rest/v1/stats/usage.json"),
                              json={"result": [{
                                  "total": 200
                              }]})
            self.client.update_calls_today()

        self.assertEqual(200, self.client.calls_today)
Exemplo n.º 5
0
    def test_upload(self, celery, session, metricsmock):
        ApiKeyFactory(valid_key="e5444-7946")
        ExportConfigFactory(
            name="test",
            batch=4,
            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"))
        reports.extend(self.add_reports(celery, 1, set_position=False))

        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 = json.loads(body)["items"]
        assert len(send_reports) == 4

        for field in ("accuracy", "source", "timestamp"):
            expect = [(report["position"] or {}).get(field)
                      for report in reports]
            gotten = [(report["position"] or {}).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"])

        assert metricsmock.has_record("incr",
                                      "data.export.batch",
                                      value=1,
                                      tags=["key:test"])
        assert metricsmock.has_record("incr",
                                      "data.export.upload",
                                      value=1,
                                      tags=["key:test", "status:200"])
        assert metricsmock.has_record("timing",
                                      "data.export.upload.timing",
                                      tags=["key:test"])
Exemplo n.º 6
0
    def test_refresh_token(self):
        with requests_mock.Mocker(real_http=True) as mock:
            mock.register_uri("GET",
                              self.client.get_url("identity/oauth/token"),
                              json={
                                  "access_token": "token",
                                  "expires_in": 1800
                              })
            self.client.refresh_token()

        expires = pendulum.datetime(2017, 1, 1).add(seconds=1800 - 15)
        self.assertEqual("token", self.client.access_token)
        self.assertEqual(expires, self.client.token_expires)
Exemplo n.º 7
0
 def test_test_corona_unsupported(self):
     # disable refresh_token being called
     self.client.token_expires = pendulum.utcnow().add(days=1)
     # disable calls_today
     self.client.calls_today = 1
     with requests_mock.Mocker(real_http=True) as mock:
         create = self.client.get_bulk_endpoint("leads", "create")
         cancel = self.client.get_bulk_endpoint("leads", "cancel", "123")
         mock.register_uri("POST",
                           self.client.get_url(create),
                           json={"errors": [{
                               "code": "1035"
                           }]})
         self.assertFalse(self.client.use_corona)
Exemplo n.º 8
0
    def test_is_auth_not_required_should_return_false_when_status_code_of_encrypt_api_is_not_401(self):
        authorization = str(base64.b64encode("{}:{}".format(self.USERNAME, self.PASSWORD).encode("utf-8")), 'utf-8')
        request_headers = {
            'Accept': 'application/vnd.go.cd.v1+json',
            'Content-Type': 'application/json',
            'Authorization': "Basic {}".format(authorization)
        }

        with requests_mock.Mocker() as mock:
            mock.register_uri('POST',
                              "{}{}".format(self.DOMAIN, "/go/api/admin/encrypt"),
                              request_headers=request_headers,
                              json={"value": "AES:encrypted-text"},
                              status_code=200)
            gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN, self.ENTITY_ID, self.USERNAME,
                                                       self.PASSWORD)
            assert not gocd_api_request.is_auth_not_required()
Exemplo n.º 9
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.º 10
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.º 11
0
    def test_is_auth_not_required_should_return_true_when_status_code_of_encrypt_api_is_401_with_message(self):
        authorization = str(base64.b64encode("{}:{}".format(self.USERNAME, self.PASSWORD).encode("utf-8")), 'utf-8')
        request_headers = {
            'Accept': 'application/vnd.go.cd.v1+json',
            'Content-Type': 'application/json',
            'Authorization': "Basic {}".format(authorization)
        }

        with requests_mock.Mocker() as mock:
            mock.register_uri('POST',
                              "{}{}".format(self.DOMAIN, "/go/api/admin/encrypt"),
                              request_headers=request_headers,
                              json={
                                  "message": "Basic authentication credentials are not required, since security has been disabled on this server."
                              },
                              status_code=401)
            gocd_api_request = gocd_api.GocdApiRequest(self.CONFIG_TYPE, self.DOMAIN, self.ENTITY_ID, self.USERNAME,
                                                       self.PASSWORD)
            assert gocd_api_request.is_auth_not_required()
Exemplo n.º 12
0
 def test_test_corona(self):
     # disable refresh_token being called
     self.client.token_expires = pendulum.utcnow().add(days=1)
     # disable calls_today
     self.client.calls_today = 1
     with requests_mock.Mocker(real_http=True) as mock:
         create = self.client.get_bulk_endpoint("leads", "create")
         cancel = self.client.get_bulk_endpoint("leads", "cancel", "123")
         mock.register_uri("POST",
                           self.client.get_url(create),
                           json={
                               "success": True,
                               "result": [{
                                   "exportId": "123"
                               }]
                           })
         mock.register_uri("POST",
                           self.client.get_url(cancel),
                           json={"success": True})
         self.assertTrue(self.client.use_corona)
Exemplo n.º 13
0
    def test_calls_today_updates(self):
        # disable refresh_token being called
        self.client.token_expires = pendulum.utcnow().add(days=1)
        # sanity check - make sure we don't have any calls yet
        self.assertEqual(0, self.client.calls_today)
        with requests_mock.Mocker(real_http=True) as mock:
            # the endpoitn we're going to call to make sure call count was updated
            mock.register_uri("GET",
                              self.client.get_url("what"),
                              json={"success": True})
            # mock out the call count endpoint
            mock.register_uri("GET",
                              self.client.get_url("rest/v1/stats/usage.json"),
                              json={"result": [{
                                  "total": 200
                              }]})
            # make the request
            self.client.request("GET", "what")

        # call count should be updated
        self.assertEqual(201, self.client.calls_today)
Exemplo n.º 14
0
    def test_expired_token_refreshes(self):
        # make sure token looks expired
        self.client.token_expires = pendulum.utcnow().subtract(days=1)
        # make sure calls_today doesn't update
        self.client.calls_today = 1
        with requests_mock.Mocker(real_http=True) as mock:
            # the endpoitn we're going to call to make sure refresh_token gets called
            mock.register_uri("GET",
                              self.client.get_url("what"),
                              json={"success": True})
            # mock out the refresh_token endpoint
            mock.register_uri("GET",
                              self.client.get_url("identity/oauth/token"),
                              json={
                                  "access_token": "token",
                                  "expires_in": 1800
                              })
            # make the request
            self.client.request("GET", "what")

        self.assertEqual("token", self.client.access_token)
Exemplo n.º 15
0
    def test_get_apps_use_cache(self, mock):
        mock.register_uri('GET', "http://*****:*****@some-marathon.com/v2/apps", text=self.marathon_apps_json)
        self.assertEquals(self.marathon_apps["apps"], update.get_apps(self.marathon))
        self.assertCountEqual(json.dumps(self.marathon_apps['apps']),
                              config.rdb.get(self.marathon['host'] + '-cache').decode())
        self.assertEquals(None, config.rdb.get(self.marathon['host'] + '-errors'))

        mock.register_uri('GET', "http://*****:*****@some-marathon.com/v2/apps", text="some html",
                          status_code=401)
        self.assertEquals(self.marathon_apps["apps"], update.get_apps(self.marathon))
        self.assertCountEqual(json.dumps(self.marathon_apps['apps']),
                              config.rdb.get(self.marathon['host'] + '-cache').decode())
        self.assertEquals(b'could not read marathon: some-marathon.com [ JSONDecodeError ]',
                          config.rdb.get(self.marathon['host'] + '-errors'))

        mock.register_uri('GET', "http://*****:*****@some-marathon.com/v2/apps", text=self.marathon_apps_json)
        self.assertEquals(self.marathon_apps["apps"], update.get_apps(self.marathon))
        self.assertCountEqual(json.dumps(self.marathon_apps['apps']),
                              config.rdb.get(self.marathon['host'] + '-cache').decode())
        self.assertEquals(None, config.rdb.get(self.marathon['host'] + '-errors'))
Exemplo n.º 16
0
 def test_get_apps_200(self, mock):
     mock.register_uri('GET', "http://*****:*****@some-marathon.com/v2/apps", text=self.marathon_apps_json)
     self.assertEquals(self.marathon_apps["apps"], update.get_apps(self.marathon))
     self.assertCountEqual(json.dumps(self.marathon_apps['apps']),
                           config.rdb.get(self.marathon['host'] + '-cache').decode())
     self.assertEquals(None, config.rdb.get(self.marathon['host'] + '-errors'))