~allanlesage/uci-engine/coverage-extractor

« back to all changes in this revision

Viewing changes to gatekeeper/gatekeeper/resources/tests/test_api.py

  • Committer: Celso Providelo
  • Date: 2014-04-25 12:34:29 UTC
  • mto: (450.1.2 uci-engine-gatekeeper)
  • mto: This revision was merged to the branch mainline in revision 456.
  • Revision ID: celso.providelo@canonical.com-20140425123429-qcmfjm9wyptol4xi
Including gatekeeper component

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""Restful API test suite."""
 
3
from __future__ import unicode_literals
 
4
 
 
5
from testtools import TestCase
 
6
from webtest import TestApp
 
7
 
 
8
from gatekeeper.resources.root import make_app
 
9
 
 
10
 
 
11
class FakeObject(object):
 
12
 
 
13
    def __init__(self, title='A_OBJ'):
 
14
        self.title = title
 
15
 
 
16
 
 
17
class FakeSandbox(object):
 
18
 
 
19
    def __init__(self, identifier='A_UUID', title='A_OBJ'):
 
20
        self.identifier = identifier
 
21
        self.title = title
 
22
        self.objects = [FakeObject('obj1'), FakeObject('obj2')]
 
23
 
 
24
    def get_temp_url(self, obj_name, method):
 
25
        return b'{}/{}/{}'.format(method, self.identifier, obj_name)
 
26
 
 
27
    def drop(self):
 
28
        return
 
29
 
 
30
 
 
31
class FakeDataStore(object):
 
32
 
 
33
    def create_sandbox(self):
 
34
        return FakeSandbox()
 
35
 
 
36
    def get_sandbox(self, identifier):
 
37
        return FakeSandbox(identifier)
 
38
 
 
39
 
 
40
def fake_datastore_factory(klass):
 
41
    return FakeDataStore()
 
42
 
 
43
 
 
44
class TestAPIv1(TestCase):
 
45
 
 
46
    def setUp(self):
 
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='.')
 
51
 
 
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)
 
58
 
 
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)
 
68
 
 
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)
 
73
 
 
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)
 
80
 
 
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)
 
87
 
 
88
    def test_put_object(self):
 
89
        # PUT on a sandbox object URL returns a tempurl for
 
90
        # uploading it.
 
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)
 
95
 
 
96
    def test_get_object(self):
 
97
        # GET on a sandbox object URL returns a tempurl for
 
98
        # downloading it.
 
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)