Skip to content
Snippets Groups Projects
test_resource.py 4.19 KiB
Newer Older
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from unittest import TestCase
from pod_client.resource import Resource, API_URLS
HOST = 'http://localhost:8080'
API_KEY = os.environ.get('POD_UNIT_TEST_API_KEY')


def setUpModule():
    pass


def tearDownModule():
    pass


class ResourceTest(TestCase):

    def test_init(self):
        resource_name = 'videos'
        resource_file = Resource(HOST, API_KEY, resource_name)
        self.assertEqual(HOST, resource_file.host_url)
        self.assertEqual(resource_name, resource_file.name)
        success = True
        try:
            resource_file = Resource('', API_KEY, resource_name)
        except Exception:
            success = False
        self.assertFalse(success)
        success = True
        try:
            resource_file = Resource(HOST, API_KEY, 'fake')
        except Exception:
            success = False
        self.assertFalse(success)
        success = True
        try:
            resource_file = Resource(HOST, '', 'fake')
        except Exception:
            success = False
        self.assertFalse(success)

    def test_url(self):
        for resource_name in API_URLS:
            resource = Resource(HOST, API_KEY, resource_name)
            url = '%s%s' % (resource.host_url, API_URLS[resource_name])
            url_with_resource = url + '1/'
            self.assertEqual(resource.url(), url)
            self.assertEqual(resource.url(1), url_with_resource)

        for resource_name in API_URLS:
            if resource_name in ['launch_encode_view']:
                continue
            resource = Resource(HOST, API_KEY, resource_name)
            response = resource.get()
            self.assertTrue(response['success'], response.get('error'))
            if response['data'].get('results', []):
                first_id = response['data'].get('results', [])[0].get('id')
                if not first_id:
                    # specific case for sites
                    first_id = response['data'].get('results', [])[0].get('url').split('/')[-2]
                self.assertTrue(first_id)
                response = resource.get(resource_id=first_id)
                self.assertTrue(response['success'], response.get('error'))
        resource = Resource(HOST, API_KEY, 'channels')
        response = resource.post(data={'title': 'unittest'})
        self.assertTrue(response['success'], response.get('error'))
        self.assertTrue(response['data']['id'])
        response = resource.delete(resource_id=response['data']['id'])
        self.assertTrue(response['success'], response.get('error'))
        resource = Resource(HOST, API_KEY, 'channels')
        response = resource.post(data={'title': 'unittest'})
        self.assertTrue(response['success'], response.get('error'))
        response['data']['title'] = 'New title unittest'
        response = resource.put(resource_id=response['data']['id'], data=response['data'])
        self.assertTrue(response['success'], response.get('error'))
        self.assertEqual(response['data']['title'], 'New title unittest')
        response = resource.delete(resource_id=response['data']['id'])
        self.assertTrue(response['success'], response.get('error'))
        resource = Resource(HOST, API_KEY, 'channels')
        response = resource.post(data={'title': 'unittest'})
        self.assertTrue(response['success'], response.get('error'))
        response = resource.patch(resource_id=response['data']['id'], data={'title': 'New title unittest'})
        self.assertTrue(response['success'], response.get('error'))
        self.assertEqual(response['data']['title'], 'New title unittest')
        response = resource.delete(resource_id=response['data']['id'])
        self.assertTrue(response['success'], response.get('error'))
        resource = Resource(HOST, API_KEY, 'channels')
        response = resource.post(data={'title': 'unittest'})
        self.assertTrue(response['success'], response.get('error'))
        self.assertTrue(response['data']['id'])
        response = resource.delete(resource_id=response['data']['id'])
        self.assertTrue(response['success'], response.get('error'))