aboutsummaryrefslogtreecommitdiffstats
path: root/src/testapp/tests.py
blob: ec1607ad1405bc23464087728510966f020bbcb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
This file demonstrates two different styles of tests (one doctest and one
unittest). These will both pass when you run "manage.py test".

Replace these with more appropriate tests for your application.
"""

from django.test import TestCase
from django.core.urlresolvers import reverse
from testapp import views
import json
#from rest.utils import xml2dict, dict2xml


class AcceptHeaderTests(TestCase):
    def assert_accept_mimetype(self, mimetype, expect=None, expect_match=True):
        """
        Assert that a request with given mimetype in the accept header,
        gives a response with the appropriate content-type.
        """
        if expect is None:
            expect = mimetype

        resp = self.client.get(reverse(views.ReadOnlyResource), HTTP_ACCEPT=mimetype)

        if expect_match:
            self.assertEquals(resp['content-type'], expect)
        else:
            self.assertNotEquals(resp['content-type'], expect)

    def test_accept_xml(self):
        self.assert_accept_mimetype('application/xml')

    def test_accept_json(self):
        self.assert_accept_mimetype('application/json')

    def test_accept_xml_prefered_to_json(self):
        self.assert_accept_mimetype('application/xml,q=0.9;application/json,q=0.1', expect='application/xml')

    def test_accept_json_prefered_to_xml(self):
        self.assert_accept_mimetype('application/json,q=0.9;application/xml,q=0.1', expect='application/json')

    def test_dont_accept_invalid(self):
        self.assert_accept_mimetype('application/invalid', expect_match=False)

    def test_invalid_accept_header_returns_406(self):
        resp = self.client.get(reverse(views.ReadOnlyResource), HTTP_ACCEPT='invalid/invalid')
        self.assertEquals(resp.status_code, 406)
    
    def test_prefer_specific(self):
        self.fail("Test not implemented")


class AllowedMethodsTests(TestCase):
    def test_reading_read_only_allowed(self):
        resp = self.client.get(reverse(views.ReadOnlyResource))
        self.assertEquals(resp.status_code, 200)
        
    def test_writing_read_only_not_allowed(self):
        resp = self.client.put(reverse(views.ReadOnlyResource), {})
        self.assertEquals(resp.status_code, 405)

    def test_reading_write_only_not_allowed(self):
        resp = self.client.get(reverse(views.WriteOnlyResource))
        self.assertEquals(resp.status_code, 405)

    def test_writing_write_only_allowed(self):
        resp = self.client.put(reverse(views.WriteOnlyResource), {})
        self.assertEquals(resp.status_code, 200)


class EncodeDecodeTests(TestCase):
    def setUp(self):
        super(self.__class__, self).setUp()
        self.input = {'a': 1, 'b': 'example'}

    def test_encode_form_decode_json(self):
        content = self.input
        resp = self.client.put(reverse(views.WriteOnlyResource), content)
        output = json.loads(resp.content)
        self.assertEquals(self.input, output)

    def test_encode_json_decode_json(self):
        content = json.dumps(self.input)
        resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json')
        output = json.loads(resp.content)
        self.assertEquals(self.input, output)

    #def test_encode_xml_decode_json(self):
    #    content = dict2xml(self.input)
    #    resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/json')
    #    output = json.loads(resp.content)
    #    self.assertEquals(self.input, output)

    #def test_encode_form_decode_xml(self):
    #    content = self.input
    #    resp = self.client.put(reverse(views.WriteOnlyResource), content, HTTP_ACCEPT='application/xml')
    #    output = xml2dict(resp.content)
    #    self.assertEquals(self.input, output)

    #def test_encode_json_decode_xml(self):
    #    content = json.dumps(self.input)
    #    resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/xml')
    #    output = xml2dict(resp.content)
    #    self.assertEquals(self.input, output)

    #def test_encode_xml_decode_xml(self):
    #    content = dict2xml(self.input)
    #    resp = self.client.put(reverse(views.WriteOnlyResource), content, 'application/json', HTTP_ACCEPT='application/xml')
    #    output = xml2dict(resp.content)
    #    self.assertEquals(self.input, output)

class ModelTests(TestCase):
    def test_create_container(self):
        content = json.dumps({'name': 'example'})
        resp = self.client.post(reverse(views.ContainerFactory), content, 'application/json')
        output = json.loads(resp.content)
        self.assertEquals(resp.status_code, 201)
        self.assertEquals(output['name'], 'example')
        self.assertEquals(set(output.keys()), set(('absolute_uri', 'name', 'key')))

class CreatedModelTests(TestCase):
    def setUp(self):
        content = json.dumps({'name': 'example'})
        resp = self.client.post(reverse(views.ContainerFactory), content, 'application/json', HTTP_ACCEPT='application/json')
        self.container = json.loads(resp.content)

    def test_read_container(self):
        resp = self.client.get(self.container["absolute_uri"])
        self.assertEquals(resp.status_code, 200)
        container = json.loads(resp.content)
        self.assertEquals(container, self.container)

    def test_delete_container(self):
        resp = self.client.delete(self.container["absolute_uri"])
        self.assertEquals(resp.status_code, 204)
        self.assertEquals(resp.content, '')

    def test_update_container(self):
        self.container['name'] = 'new'
        content = json.dumps(self.container)
        resp = self.client.put(self.container["absolute_uri"], content, 'application/json')
        self.assertEquals(resp.status_code, 200)
        container = json.loads(resp.content)
        self.assertEquals(container, self.container)