示例#1
0
 def test_duplicate_mac_update(self):
     api_creator = ResourceCreator(self.api)
     payload = copy.copy(test_variables.DEFAULT_OBJECTS['device']['payload'])
     payload['mac_address'] = '00:1F:F3:00:1F:F3'
     api_creator.create_valid_resource('device', payload=payload)
     (dev_update, _) = api_creator.create_valid_resource('device')
     dev_info = self.api.get(dev_update['uri']).json()
     headers = {
         'Origin': dev_info['origin']
     }
     res = self.mitm.post('/autoconfig/mymac', headers=headers, data=payload['mac_address'])
     self.assertTrue(res.status_code == 422)
示例#2
0
 def test_no_configured_endpoints(self):
     api_creator = ResourceCreator(self.api)
     session_id = None
     try:
         res = self.mitm.post('/autoconfig/register')
         self.assertTrue(res.status_code == 201)
         session_id = res.content.decode('utf-8')
         (dev_info, _) = api_creator.create_valid_resource('device')
         accept_info = {
             'status': 1,
             'device_id': dev_info['uri']
         }
         with GetStorage(self.api) as storage:
             storage.upload_all()
             res = self.api.post('/api/autoconf/{}'.format(session_id), json=accept_info)
             self.assertTrue(res.status_code == 406)
             expected_issues = {
                 'X-Critical': [
                     AutoConfIssues.pd_not_configured.value,
                     AutoConfIssues.rgc_not_configured.value,
                 ],
                 'X-Warnings': [
                     AutoConfIssues.no_ggl_login.value,
                     AutoConfIssues.auth_not_configured.value,
                 ]
             }
             self.assertListEqual(expected_issues['X-Critical'], json.loads(res.headers['X-Critical']))
             self.assertListEqual(expected_issues['X-Warnings'], json.loads(res.headers['X-Warnings']))
     finally:
         api_creator.remove_resources()
         if session_id is not None:
             self.mitm.delete('/autoconfig/{}/complete'.format(session_id))
示例#3
0
 def decorated(self, *args, **kwargs):
     api_creator = ResourceCreator(self.api)
     gacct = None
     session_id = None
     with GetStorage(self.api) as storage:
         try:
             storage.upload_all()
             create_valid_configs(self, api_creator)
             res = self.mitm.post('/autoconfig/register')
             self.assertTrue(res.status_code == 201)
             session_id = res.content.decode('utf-8')
             # Create Google Account
             gacc = {
                 "login_type": "google",
                 "username": "******",
                 "password": "******"
             }
             res = self.api.post('/api/pogoauth', json=gacc)
             gacct = res.headers['X-URI']
             self.assertTrue(res.status_code == 201)
             dev_payload = copy.copy(
                 global_variables.DEFAULT_OBJECTS['device']['payload'])
             dev_payload['account_id'] = gacct
             (dev_info,
              _) = api_creator.create_valid_resource('device',
                                                     payload=dev_payload)
             accept_info = {'status': 1, 'device_id': dev_info['uri']}
             res = self.api.post('/api/autoconf/{}'.format(session_id),
                                 json=accept_info)
             self.assertTrue(res.status_code == 200)
             res = self.mitm.get('/autoconfig/{}/status'.format(session_id))
             self.assertTrue(res.status_code == 200)
             (ss_info,
              _) = api_creator.create_valid_resource('devicesetting')
             func(self, session_id, dev_info, ss_info, api_creator, *args,
                  **kwargs)
         except Exception:
             raise
         finally:
             self.api.delete('/api/autoconf/rgc')
             self.api.delete('/api/autoconf/pd')
             api_creator.remove_resources()
             if session_id is not None:
                 self.mitm.delete(
                     '/autoconfig/{}/complete'.format(session_id))
             if gacct is not None:
                 self.api.delete(gacct)
示例#4
0
class APITestBase(TestCase, object):
    generated_uris = []
    index = 0

    def setUp(self):
        self.api = LocalAPI()
        self.creator = ResourceCreator(self.api)

    def tearDown(self):
        self.creator.remove_resources()
        self.api.close()

    def create_resource(self, uri, payload, **kwargs):
        response = self.creator.create_resource(uri, payload, **kwargs)
        self.assertEqual(response.status_code, 201)
        return response

    def delete_resource(self, uri):
        return self.creator.delete_resource(uri)

    def create_valid(self, payload, uri=None, **kwargs):
        uri = uri if uri else self.uri
        return self.create_resource(uri, payload, **kwargs)

    def remove_resources(self):
        self.creator.remove_resources()

    # ===========================
    # ===== Basic Resources =====
    # ===========================
    def create_valid_resource(self, resource, **kwargs):
        elem, response = self.creator.create_valid_resource(resource, **kwargs)
        self.assertEqual(response.status_code, 201)
        elem['uri'] = response.headers['X-Uri']
        return elem

    def get_valid_resource(self, resource, **kwargs):
        return self.creator.get_valid_resource(resource, **kwargs)

    # ===========================
    # ========== Tests ==========
    # ===========================
    def landing_page(self, test_resource=True):
        response = self.api.get(self.uri)
        self.assertEqual(response.status_code, 200)
        json_data = response.json()
        self.assertTrue('resource' in json_data)
        self.assertTrue('results' in json_data)
        if test_resource:
            self.assertTrue(len(json_data['resource']['fields']) > 0)
            if 'settings' in json_data['resource']:
                self.assertTrue(len(json_data['resource']['settings']) > 0)
        params = {'hide_resource': 1}
        response = self.api.get(self.uri, params=params)
        self.assertEqual(response.status_code, 200)
        self.assertFalse('resource' in response.json())
        self.assertTrue(type(response.json()) is dict)
        params['fetch_all'] = 1
        response = self.api.get(self.uri, params=params)
        self.assertEqual(response.status_code, 200)
        for key, value in response.json().items():
            self.assertTrue(type(value) is dict)

    def invalid_uri(self):
        path = '%s/%s' % (self.uri, '-1')
        response = self.api.get(path)
        self.assertEqual(response.status_code, 404)

    def invalid_post(self, payload, errors, error_code=422, **kwargs):
        response = self.api.post(self.uri, json=payload, **kwargs)
        self.assertEqual(response.status_code, error_code)
        self.assertDictEqual(response.json(), errors)

    def valid_post(self, payload, result, **kwargs):
        response = self.create_valid(payload, **kwargs)
        self.assertEqual(response.status_code, 201)
        self.assertDictEqual(result, response.json())
        uri = response.headers['X-Uri']
        response = self.delete_resource(uri)
        self.assertEqual(response.status_code, 202)

    def invalid_put(self, payload, errors, error_code=422, **kwargs):
        response = self.create_valid(self.base_payload)
        uri = response.headers['X-Uri']
        response = self.api.put(uri, json=payload, **kwargs)
        self.assertEqual(response.status_code, error_code)
        self.assertDictEqual(response.json(), errors)
        response = self.delete_resource(uri)
        self.assertEqual(response.status_code, 202)

    def valid_put(self, payload, result, **kwargs):
        response = self.create_valid(self.base_payload)
        uri = response.headers['X-Uri']
        response = self.api.put(uri, json=payload, **kwargs)
        self.assertEqual(response.status_code, 204)
        response = self.api.get(uri)
        self.assertDictEqual(result, response.json())
        response = self.delete_resource(uri)

    def invalid_patch(self, payload, errors, error_code=422, **kwargs):
        response = self.create_valid(self.base_payload)
        uri = response.headers['X-Uri']
        response = self.api.patch(uri, json=payload, **kwargs)
        self.assertEqual(response.status_code, error_code)
        self.assertDictEqual(errors, response.json())
        response = self.delete_resource(uri)

    def valid_patch(self, payload, result, original=None, **kwargs):
        base_payload = original if original else self.base_payload
        response = self.create_valid(base_payload)
        uri = response.headers['X-Uri']
        response = self.api.patch(uri, json=payload, **kwargs)
        self.assertEqual(response.status_code, 204)
        response = self.api.get(uri)
        self.assertDictEqual(result, response.json())
        response = self.delete_resource(uri)
示例#5
0
        t_madmin.start()

    logger.info("MAD is now running.....")
    exit_code = 0
    device_creator = None
    try:
        if args.unit_tests:
            from mapadroid.tests.local_api import LocalAPI
            api_ready = False
            api = LocalAPI()
            logger.info('Checking API status')
            if not data_manager.get_root_resource('device').keys():
                from mapadroid.tests.test_utils import ResourceCreator
                logger.info('Creating a device')
                device_creator = ResourceCreator(api, prefix='MADCore')
                res = device_creator.create_valid_resource('device')[0]
                mapping_manager.update()
            max_attempts = 30
            attempt = 0
            while not api_ready and attempt < max_attempts:
                try:
                    api.get('/api')
                    api_ready = True
                    logger.info('API is ready for unit testing')
                except Exception:
                    attempt += 1
                    time.sleep(1)
            if api_ready:
                loader = unittest.TestLoader()
                start_dir = 'mapadroid/tests/'
                suite = loader.discover(start_dir)