1"""
2    SoftLayer.tests.CLI.modules.cdn_tests
3    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5    :license: MIT, see LICENSE for more details.
6"""
7import json
8
9from SoftLayer.CLI import exceptions
10from SoftLayer import testing
11
12
13class CdnTests(testing.TestCase):
14
15    def test_list_accounts(self):
16        result = self.run_command(['cdn', 'list'])
17
18        self.assert_no_fail(result)
19        self.assertEqual(json.loads(result.output),
20                         [{'cname': 'cdnakauuiet7s6u6.cdnedge.bluemix.net',
21                           'domain': 'test.example.com',
22                           'origin': '1.1.1.1',
23                           'status': 'CNAME_CONFIGURATION',
24                           'unique_id': '9934111111111',
25                           'vendor': 'akamai'}]
26                         )
27
28    def test_detail_account(self):
29        result = self.run_command(['cdn', 'detail', '--history=30', '1245'])
30
31        self.assert_no_fail(result)
32        self.assertEqual(json.loads(result.output),
33                         {'hit_radio': '0.0 %',
34                          'hostname': 'test.example.com',
35                          'origin': '1.1.1.1',
36                          'origin_type': 'HOST_SERVER',
37                          'path': '/',
38                          'protocol': 'HTTP',
39                          'provider': 'akamai',
40                          'status': 'CNAME_CONFIGURATION',
41                          'total_bandwidth': '0.0 GB',
42                          'total_hits': '0',
43                          'unique_id': '9934111111111'}
44                         )
45
46    def test_purge_content(self):
47        result = self.run_command(['cdn', 'purge', '1234',
48                                   '/article/file.txt'])
49
50        self.assert_no_fail(result)
51
52    def test_list_origins(self):
53        result = self.run_command(['cdn', 'origin-list', '1234'])
54
55        self.assert_no_fail(result)
56        self.assertEqual(json.loads(result.output), [{'HTTP Port': 80,
57                                                      'Origin': '10.10.10.1',
58                                                      'Path': '/example',
59                                                      'Status': 'RUNNING'},
60                                                     {'HTTP Port': 80,
61                                                      'Origin': '10.10.10.1',
62                                                      'Path': '/example1',
63                                                      'Status': 'RUNNING'}])
64
65    def test_add_origin_server(self):
66        result = self.run_command(
67            ['cdn', 'origin-add', '-t', 'server', '-H=test.example.com', '-p', 80, '-o', 'web', '-c=include-all',
68             '1234', '10.10.10.1', '/example/videos2'])
69
70        self.assert_no_fail(result)
71
72    def test_add_origin_storage(self):
73        result = self.run_command(['cdn', 'origin-add', '-t', 'storage', '-b=test-bucket', '-H=test.example.com',
74                                   '-p', 80, '-o', 'web', '-c=include-all', '1234', '10.10.10.1', '/example/videos2'])
75
76        self.assert_no_fail(result)
77
78    def test_add_origin_without_storage(self):
79        result = self.run_command(['cdn', 'origin-add', '-t', 'storage', '-H=test.example.com', '-p', 80,
80                                   '-o', 'web', '-c=include-all', '1234', '10.10.10.1', '/example/videos2'])
81
82        self.assertEqual(result.exit_code, 2)
83        self.assertIsInstance(result.exception, exceptions.ArgumentError)
84
85    def test_add_origin_storage_with_file_extensions(self):
86        result = self.run_command(
87            ['cdn', 'origin-add', '-t', 'storage', '-b=test-bucket', '-e', 'jpg', '-H=test.example.com', '-p', 80,
88             '-o', 'web', '-c=include-all', '1234', '10.10.10.1', '/example/videos2'])
89
90        self.assert_no_fail(result)
91
92    def test_remove_origin(self):
93        result = self.run_command(['cdn', 'origin-remove', '1234',
94                                   '/example1'])
95
96        self.assert_no_fail(result)
97        self.assertEqual(result.output, "Origin with path /example1 has been deleted\n")
98
99    def test_edit_header(self):
100        result = self.run_command(['cdn', 'edit', 'test.example.com', '--header=www.test.com'])
101        self.assert_no_fail(result)
102        header_result = json.loads(result.output)
103        self.assertEqual('www.test.com', header_result['Header'])
104
105    def test_edit_http_port(self):
106        result = self.run_command(['cdn', 'edit', 'test.example.com', '--http-port=83'])
107        self.assert_no_fail(result)
108        header_result = json.loads(result.output)
109        self.assertEqual(83, header_result['Http Port'])
110
111    def test_edit_respect_headers(self):
112        result = self.run_command(['cdn', 'edit', 'test.example.com', '--respect-headers=1'])
113        self.assert_no_fail(result)
114        header_result = json.loads(result.output)
115        self.assertEqual(True, header_result['Respect Headers'])
116
117    def test_edit_cache(self):
118        result = self.run_command(['cdn', 'edit', 'test.example.com', '--cache', 'include-specified',
119                                   '--cache', 'test'])
120        self.assert_no_fail(result)
121        header_result = json.loads(result.output)
122        self.assertEqual('include: test', header_result['Cache key optimization'])
123
124    def test_edit_cache_by_uniqueId(self):
125        result = self.run_command(['cdn', 'edit', '9934111111111', '--cache', 'include-specified', '--cache', 'test'])
126        self.assert_no_fail(result)
127        header_result = json.loads(result.output)
128        self.assertEqual('include: test', header_result['Cache key optimization'])
129