1
# -*- coding: utf-8 -*-
2
"""Restful API test suite."""
3
from __future__ import unicode_literals
5
from testtools import TestCase
6
from webtest import TestApp
8
from gatekeeper.resources.root import make_app
11
class FakeObject(object):
13
def __init__(self, title='A_OBJ'):
17
class FakeSandbox(object):
19
def __init__(self, identifier='A_UUID', title='A_OBJ'):
20
self.identifier = identifier
22
self.objects = [FakeObject('obj1'), FakeObject('obj2')]
24
def get_temp_url(self, obj_name, method):
25
return b'{}/{}/{}'.format(method, self.identifier, obj_name)
31
class FakeDataStore(object):
33
def create_sandbox(self):
36
def get_sandbox(self, identifier):
37
return FakeSandbox(identifier)
40
def fake_datastore_factory(klass):
41
return FakeDataStore()
44
class TestAPIv1(TestCase):
47
# Run the restful server.
48
super(TestAPIv1, self).setUp()
49
wsgi_app = make_app(fake_datastore_factory)
50
self.app = TestApp(wsgi_app, relative_to='.')
52
def test_get_greeting(self):
53
# GET on the root URL returns a greeting string as plain/text.
54
resp = self.app.get('/api/v1/')
55
self.assertEqual(200, resp.status_code)
56
self.assertEqual('text/plain', resp.content_type)
57
self.assertEqual('CI-Airline gatekeeper.\n', resp.body)
59
def test_post_creates_a_sandbox(self):
60
# POST on the root URL creates a new sandbox and returns
61
# status 201, 'Location' header and body (as 'text/plain')
62
# container the created sandbox path.
63
resp = self.app.post('/api/v1/')
64
self.assertEqual(201, resp.status_code)
65
self.assertEqual('/api/v1/sandbox/A_UUID', resp.location)
66
self.assertEqual('text/plain', resp.content_type)
67
self.assertEqual('/api/v1/sandbox/A_UUID', resp.body)
69
def test_other_methods_not_allowed(self):
70
# PUT and DELETE are not allowed.
71
self.app.put('/api/v1/', status=405)
72
self.app.delete('/api/v1/', status=405)
74
def test_get_sandbox_contents(self):
75
# GET on a sandbox URL lists its contents
76
resp = self.app.get('/api/v1/sandbox/A_UUID')
77
self.assertEqual(200, resp.status_code)
78
self.assertEqual('text/plain', resp.content_type)
79
self.assertEqual('obj1\nobj2', resp.body)
81
def test_delete_sandbox(self):
82
# DELETE on a sandbox URL deletes the sandbox and its contents.
83
resp = self.app.delete('/api/v1/sandbox/A_UUID')
84
self.assertEqual(200, resp.status_code)
85
self.assertEqual('text/plain', resp.content_type)
86
self.assertEqual('', resp.body)
88
def test_put_object(self):
89
# PUT on a sandbox object URL returns a tempurl for
91
resp = self.app.put('/api/v1/sandbox/A_UUID/A_OBJ')
92
self.assertEqual(200, resp.status_code)
93
self.assertEqual('text/plain', resp.content_type)
94
self.assertEqual('PUT/A_UUID/A_OBJ', resp.body)
96
def test_get_object(self):
97
# GET on a sandbox object URL returns a tempurl for
99
resp = self.app.get('/api/v1/sandbox/A_UUID/A_OBJ')
100
self.assertEqual(200, resp.status_code)
101
self.assertEqual('text/plain', resp.content_type)
102
self.assertEqual('GET/A_UUID/A_OBJ', resp.body)