summaryrefslogtreecommitdiff
path: root/tests/unit/s3/test_cors_configuration.py
blob: 8f6980364ca2c93a60898575ba4e6084c95ecdf6 (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
#!/usr/bin/env python

import unittest
from boto.s3.cors import CORSConfiguration

CORS_BODY_1 = (
    '<CORSConfiguration>'
    '<CORSRule>'
    '<AllowedMethod>PUT</AllowedMethod>'
    '<AllowedMethod>POST</AllowedMethod>'
    '<AllowedMethod>DELETE</AllowedMethod>'
    '<AllowedOrigin>http://www.example.com</AllowedOrigin>'
    '<AllowedHeader>*</AllowedHeader>'
    '<ExposeHeader>x-amz-server-side-encryption</ExposeHeader>'
    '<MaxAgeSeconds>3000</MaxAgeSeconds>'
    '<ID>foobar_rule</ID>'
    '</CORSRule>'
    '</CORSConfiguration>')

CORS_BODY_2 = (
    '<CORSConfiguration>'
    '<CORSRule>'
    '<AllowedMethod>PUT</AllowedMethod>'
    '<AllowedMethod>POST</AllowedMethod>'
    '<AllowedMethod>DELETE</AllowedMethod>'
    '<AllowedOrigin>http://www.example.com</AllowedOrigin>'
    '<AllowedHeader>*</AllowedHeader>'
    '<ExposeHeader>x-amz-server-side-encryption</ExposeHeader>'
    '<MaxAgeSeconds>3000</MaxAgeSeconds>'
    '</CORSRule>'
    '<CORSRule>'
    '<AllowedMethod>GET</AllowedMethod>'
    '<AllowedOrigin>*</AllowedOrigin>'
    '<AllowedHeader>*</AllowedHeader>'
    '<MaxAgeSeconds>3000</MaxAgeSeconds>'
    '</CORSRule>'
    '</CORSConfiguration>')

CORS_BODY_3 = (
    '<CORSConfiguration>'
    '<CORSRule>'
    '<AllowedMethod>GET</AllowedMethod>'
    '<AllowedOrigin>*</AllowedOrigin>'
    '</CORSRule>'
    '</CORSConfiguration>')


class TestCORSConfiguration(unittest.TestCase):

    def test_one_rule_with_id(self):
        cfg = CORSConfiguration()
        cfg.add_rule(['PUT', 'POST', 'DELETE'],
                     'http://www.example.com',
                     allowed_header='*',
                     max_age_seconds=3000,
                     expose_header='x-amz-server-side-encryption',
                     id='foobar_rule')
        self.assertEqual(cfg.to_xml(), CORS_BODY_1)

    def test_two_rules(self):
        cfg = CORSConfiguration()
        cfg.add_rule(['PUT', 'POST', 'DELETE'],
                     'http://www.example.com',
                     allowed_header='*',
                     max_age_seconds=3000,
                     expose_header='x-amz-server-side-encryption')
        cfg.add_rule('GET', '*', allowed_header='*', max_age_seconds=3000)
        self.assertEqual(cfg.to_xml(), CORS_BODY_2)

    def test_minimal(self):
        cfg = CORSConfiguration()
        cfg.add_rule('GET', '*')
        self.assertEqual(cfg.to_xml(), CORS_BODY_3)


if __name__ == "__main__":
    unittest.main()