~tribaal/txaws/xss-hardening

« back to all changes in this revision

Viewing changes to txaws/tests/test_exception.py

  • Committer: Duncan McGreggor
  • Date: 2009-11-22 02:20:42 UTC
  • mto: (44.3.2 484858-s3-scripts)
  • mto: This revision was merged to the branch mainline in revision 52.
  • Revision ID: duncan@canonical.com-20091122022042-4zi231hxni1z53xd
* Updated the LICENSE file with copyright information.
* Updated the README with license information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (c) 2009 Canonical Ltd <duncan.mcgreggor@canonical.com>
2
 
# Licenced under the txaws licence available at /LICENSE in the txaws source.
3
 
 
4
 
from twisted.trial.unittest import TestCase
5
 
 
6
 
from txaws.exception import AWSError
7
 
from txaws.exception import AWSResponseParseError
8
 
from txaws.util import XML
9
 
 
10
 
 
11
 
REQUEST_ID = "0ef9fc37-6230-4d81-b2e6-1b36277d4247"
12
 
 
13
 
 
14
 
class AWSErrorTestCase(TestCase):
15
 
 
16
 
    def test_creation(self):
17
 
        error = AWSError("<dummy1 />", 500, "Server Error", "<dummy2 />")
18
 
        self.assertEquals(error.status, 500)
19
 
        self.assertEquals(error.response, "<dummy2 />")
20
 
        self.assertEquals(error.original, "<dummy1 />")
21
 
        self.assertEquals(error.errors, [])
22
 
        self.assertEquals(error.request_id, "")
23
 
 
24
 
    def test_node_to_dict(self):
25
 
        xml = "<parent><child1>text1</child1><child2>text2</child2></parent>"
26
 
        error = AWSError("<dummy />", 400)
27
 
        data = error._node_to_dict(XML(xml))
28
 
        self.assertEquals(data, {"child1": "text1", "child2": "text2"})
29
 
 
30
 
    def test_set_request_id(self):
31
 
        xml = "<a><b /><RequestID>%s</RequestID></a>" % REQUEST_ID
32
 
        error = AWSError("<dummy />", 400)
33
 
        error._set_request_id(XML(xml))
34
 
        self.assertEquals(error.request_id, REQUEST_ID)
35
 
 
36
 
    def test_set_host_id(self):
37
 
        host_id = "ASD@#FDG$E%FG"
38
 
        xml = "<a><b /><HostID>%s</HostID></a>" % host_id
39
 
        error = AWSError("<dummy />", 400)
40
 
        error._set_host_id(XML(xml))
41
 
        self.assertEquals(error.host_id, host_id)
42
 
 
43
 
    def test_set_empty_errors(self):
44
 
        xml = "<a><Errors /><b /></a>"
45
 
        error = AWSError("<dummy />", 500)
46
 
        error._set_500_error(XML(xml))
47
 
        self.assertEquals(error.errors, [])
48
 
 
49
 
    def test_set_empty_error(self):
50
 
        xml = "<a><Errors><Error /><Error /></Errors><b /></a>"
51
 
        error = AWSError("<dummy />", 500)
52
 
        error._set_500_error(XML(xml))
53
 
        self.assertEquals(error.errors, [])
54
 
 
55
 
    def test_parse_without_xml(self):
56
 
        xml = "<dummy />"
57
 
        error = AWSError(xml, 400)
58
 
        error.parse()
59
 
        self.assertEquals(error.original, xml)
60
 
 
61
 
    def test_parse_with_xml(self):
62
 
        xml1 = "<dummy1 />"
63
 
        xml2 = "<dummy2 />"
64
 
        error = AWSError(xml1, 400)
65
 
        error.parse(xml2)
66
 
        self.assertEquals(error.original, xml2)
67
 
 
68
 
    def test_parse_html(self):
69
 
        xml = "<html><body>a page</body></html>"
70
 
        self.assertRaises(AWSResponseParseError, AWSError, xml, 400)
71
 
 
72
 
    def test_empty_xml(self):
73
 
        self.assertRaises(ValueError, AWSError, "", 400)
74
 
 
75
 
    def test_no_request_id(self):
76
 
        errors = "<Errors><Error><Code /><Message /></Error></Errors>"
77
 
        xml = "<Response>%s<RequestID /></Response>" % errors
78
 
        error = AWSError(xml, 400)
79
 
        self.assertEquals(error.request_id, "")
80
 
 
81
 
    def test_no_request_id_node(self):
82
 
        errors = "<Errors><Error><Code /><Message /></Error></Errors>"
83
 
        xml = "<Response>%s</Response>" % errors
84
 
        error = AWSError(xml, 400)
85
 
        self.assertEquals(error.request_id, "")
86
 
 
87
 
    def test_no_errors_node(self):
88
 
        xml = "<Response><RequestID /></Response>"
89
 
        error = AWSError(xml, 400)
90
 
        self.assertEquals(error.errors, [])
91
 
 
92
 
    def test_no_error_node(self):
93
 
        xml = "<Response><Errors /><RequestID /></Response>"
94
 
        error = AWSError(xml, 400)
95
 
        self.assertEquals(error.errors, [])
96
 
 
97
 
    def test_no_error_code_node(self):
98
 
        errors = "<Errors><Error><Message /></Error></Errors>"
99
 
        xml = "<Response>%s<RequestID /></Response>" % errors
100
 
        error = AWSError(xml, 400)
101
 
        self.assertEquals(error.errors, [])
102
 
 
103
 
    def test_no_error_message_node(self):
104
 
        errors = "<Errors><Error><Code /></Error></Errors>"
105
 
        xml = "<Response>%s<RequestID /></Response>" % errors
106
 
        error = AWSError(xml, 400)
107
 
        self.assertEquals(error.errors, [])
108
 
 
109
 
    def test_set_500_error(self):
110
 
        xml = "<Error><Code>500</Code><Message>Oops</Message></Error>"
111
 
        error = AWSError("<dummy />", 500)
112
 
        error._set_500_error(XML(xml))
113
 
        self.assertEquals(error.errors[0]["Code"], "500")
114
 
        self.assertEquals(error.errors[0]["Message"], "Oops")