summaryrefslogtreecommitdiffstats
path: root/test/functional_auth/gswauth/test_gswauth.py
blob: 5219f13133110a516973149823a881c9832cfbc4 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/python

# Copyright (c) 2010-2012 OpenStack Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.

try:
    import simplejson as json
except ImportError:
    import json
import unittest
from nose import SkipTest
from swift.common.bufferedhttp import http_connect_raw as http_connect
from test import get_config

config = get_config('func_test')

class TestGSWauth(unittest.TestCase):

    def _get_admin_headers(self):
        return {'X-Auth-Admin-User': config['admin_user'],
                'X-Auth-Admin-Key': config['admin_key']}

    def _check_test_account_is_not_registered(self):
        # check account exists
        path = '%sv2/%s' % (config['auth_prefix'], config['account'])

        headers = self._get_admin_headers()
        headers.update({'Content-Length': '0'})
        conn = http_connect(config['auth_host'], config['auth_port'], 'GET',
                path, headers)
        resp = conn.getresponse()
        self.assertTrue(resp.status == 404)

    def _register_test_account(self):
        # create account in swauth (not a swift account)
        # This current version only supports one account per volume
        # and the account name is the same as the volume name
        # still an account must be created with swauth to map
        # swauth accounts with swift accounts
        path = '%sv2/%s' % (config['auth_prefix'], config['account'])
        headers = self._get_admin_headers()
        headers.update({'Content-Length': '0'})
        conn = http_connect(config['auth_host'], config['auth_port'], 'PUT',
                path, headers)
        resp = conn.getresponse()
        self.assertTrue(resp.status == 201)

    def _deregister_test_account(self):
        # delete account in swauth (not a swift account)
        # @see _register_test_account
        path = '%sv2/%s' % (config['auth_prefix'], config['account'])
        headers = self._get_admin_headers()
        headers.update({'Content-Length': '0'})
        conn = http_connect(config['auth_host'], config['auth_port'],
                'DELETE', path, headers)
        resp = conn.getresponse()
        self.assertTrue(resp.status == 204)

    def test_register_account(self):
        # check and register account
        self._check_test_account_is_not_registered()
        self._register_test_account()

        try:
            # list account
            path = '%sv2/%s' % (config['auth_prefix'], config['account'])
            headers = self._get_admin_headers()
            conn = http_connect(config['auth_host'], config['auth_port'],
                    'GET', path, headers)
            resp = conn.getresponse()
            body = resp.read()
            info = json.loads(body)
            self.assertEqual(info['account_id'], 'AUTH_test')
            self.assertTrue(resp.status == 200)

        finally:
            # de-register account
            self._deregister_test_account()

    def test_add_user(self):
        # check and register account
        self._check_test_account_is_not_registered()
        self._register_test_account()

        # create user
        path = '%sv2/%s/%s' % (config['auth_prefix'], config['account'],
                config['username'])
        headers = self._get_admin_headers()
        headers.update({'X-Auth-User-Key': config['password'],
                        'Content-Length': '0',
                        'X-Auth-User-Admin': 'true'})
        conn = http_connect(config['auth_host'], config['auth_port'], 'PUT',
                path, headers)
        resp = conn.getresponse()
        self.assertTrue(resp.status == 201)

        try:
            # list user
            headers = self._get_admin_headers()
            conn = http_connect(config['auth_host'], config['auth_port'],
                    'GET', path, headers)
            resp = conn.getresponse()
            body = resp.read()
            self.assertEqual(body, '{"groups": [{"name": "test:tester"}, {"name":'
                ' "test"}, {"name": ".admin"}], "auth": "plaintext:testing"}')
            self.assertTrue(resp.status == 200)

        finally:
            try:
                # delete user
                headers = self._get_admin_headers()
                conn = http_connect(config['auth_host'], config['auth_port'],
                        'DELETE', path, headers)
                resp = conn.getresponse()
                self.assertTrue(resp.status == 204)

            finally:
                # de-register account
                self._deregister_test_account()

    def test_register_invalid_account(self):
        # invalid account
        path = '%sv2/%s' % (config['auth_prefix'], '.test')
        headers = self._get_admin_headers()
        headers.update({'Content-Length': '0'})
        conn = http_connect(config['auth_host'], config['auth_port'], 'PUT',
                path, headers)
        resp = conn.getresponse()
        self.assertTrue(resp.status == 400)

    def test_add_invalid_user(self):
        path = '%sv2/%s/%s' % (config['auth_prefix'], config['account'],
                '.invaliduser')
        headers = self._get_admin_headers()
        headers.update({'X-Auth-User-Key': config['password'],
                        'Content-Length': '0',
                        'X-Auth-User-Admin': 'true'})
        conn = http_connect(config['auth_host'], config['auth_port'], 'PUT',
                path, headers)
        resp = conn.getresponse()
        self.assertTrue(resp.status == 400)

    def test_register_account_without_admin_rights(self):
        path = '%sv2/%s' % (config['auth_prefix'], config['account'])
        headers = {'X-Auth-Admin-User': config['admin_user']}
        headers.update({'Content-Length': '0'})
        conn = http_connect(config['auth_host'], config['auth_port'], 'PUT',
                path, headers)
        resp = conn.getresponse()
        self.assertTrue(resp.status == 401)

    def test_change_user_password(self):
        # check and register account
        self._check_test_account_is_not_registered()
        self._register_test_account()

        try:
            # create user
            path = '%sv2/%s/%s' % (config['auth_prefix'], config['account'],
                    config['username'])
            headers = self._get_admin_headers()
            headers.update({'X-Auth-User-Key': config['password'],
                            'Content-Length': '0',
                            'X-Auth-User-Admin': 'true'})
            conn = http_connect(config['auth_host'], config['auth_port'], 'PUT',
                    path, headers)
            resp = conn.getresponse()
            print "resp creating user %s" % resp.status
            self.assertTrue(resp.status == 201)

            # change password
            path = '%sv2/%s/%s' % (config['auth_prefix'], config['account'],
                    config['username'])
            headers = self._get_admin_headers()
            headers.update({'X-Auth-User-Key': 'newpassword',
                            'Content-Length': '0',
                            'X-Auth-User-Admin': 'true'})
            conn = http_connect(config['auth_host'], config['auth_port'], 'PUT',
                    path, headers)
            resp = conn.getresponse()
            print "resp changing password %s" % resp.status
            self.assertTrue(resp.status == 201)
        finally:
            try:
                # delete user
                headers = self._get_admin_headers()
                conn = http_connect(config['auth_host'], config['auth_port'],
                        'DELETE', path, headers)
                resp = conn.getresponse()
                self.assertTrue(resp.status == 204)

            finally:
                # de-register account
                self._deregister_test_account()

    def test_change_user_password_without_admin_rights(self):
        # check and register account
        self._check_test_account_is_not_registered()
        self._register_test_account()

        try:
            # create user
            path = '%sv2/%s/%s' % (config['auth_prefix'], config['account'],
                    config['username'])
            headers = self._get_admin_headers()
            headers.update({'X-Auth-User-Key': config['password'],
                            'Content-Length': '0',
                            'X-Auth-User-Admin': 'true'})
            conn = http_connect(config['auth_host'], config['auth_port'], 'PUT',
                    path, headers)
            resp = conn.getresponse()
            print "resp creating user %s" % resp.status
            self.assertTrue(resp.status == 201)

            # attempt to change password
            path = '%sv2/%s/%s' % (config['auth_prefix'], config['account'],
                    config['username'])
            headers = {'X-Auth-Admin-User':
                       config['account'] + ':' + config['username'],
                       'X-Auth-Admin-Key': config['password']}
            headers.update({'X-Auth-User-Key': 'newpassword',
                            'Content-Length': '0',
                            'X-Auth-User-Admin': 'true'})
            conn = http_connect(config['auth_host'], config['auth_port'], 'PUT',
                    path, headers)
            resp = conn.getresponse()
            self.assertTrue(resp.status == 201)

        finally:
            try:
                # delete user
                headers = self._get_admin_headers()
                conn = http_connect(config['auth_host'], config['auth_port'],
                        'DELETE', path, headers)
                resp = conn.getresponse()
                self.assertTrue(resp.status == 204)

            finally:
                # de-register account
                self._deregister_test_account()