Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / tests / api.py @ 2d31dfd7

History | View | Annotate | Download (23.3 kB)

1
# Copyright 2011, 2012, 2013 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
from astakos.im.tests.common import *
35
from astakos.im.settings import astakos_services, BASE_HOST
36
from synnefo.lib.services import get_service_path
37
from synnefo.lib import join_urls
38

    
39
from django.test import TestCase
40
from django.core.urlresolvers import reverse
41

    
42
#from xml.dom import minidom
43

    
44
import json
45

    
46
ROOT = "/%s/%s/%s/" % (
47
    astakos_settings.BASE_PATH, astakos_settings.ACCOUNTS_PREFIX, 'v1.0')
48
u = lambda url: ROOT + url
49

    
50

    
51
class QuotaAPITest(TestCase):
52
    def test_0(self):
53
        client = Client()
54

    
55
        component1 = Component.objects.create(name="comp1")
56
        register.add_service(component1, "service1", "type1", [])
57
        # custom service resources
58
        resource11 = {"name": "service1.resource11",
59
                      "desc": "resource11 desc",
60
                      "service_type": "type1",
61
                      "service_origin": "service1",
62
                      "allow_in_projects": True}
63
        r, _ = register.add_resource(resource11)
64
        register.update_resource(r, 100)
65
        resource12 = {"name": "service1.resource12",
66
                      "desc": "resource11 desc",
67
                      "service_type": "type1",
68
                      "service_origin": "service1",
69
                      "unit": "bytes"}
70
        r, _ = register.add_resource(resource12)
71
        register.update_resource(r, 1024)
72

    
73
        # create user
74
        user = get_local_user('test@grnet.gr')
75
        quotas.qh_sync_user(user)
76

    
77
        component2 = Component.objects.create(name="comp2")
78
        register.add_service(component2, "service2", "type2", [])
79
        # create another service
80
        resource21 = {"name": "service2.resource21",
81
                      "desc": "resource11 desc",
82
                      "service_type": "type2",
83
                      "service_origin": "service2",
84
                      "allow_in_projects": False}
85
        r, _ = register.add_resource(resource21)
86
        register.update_resource(r, 3)
87

    
88
        resource_names = [r['name'] for r in
89
                          [resource11, resource12, resource21]]
90

    
91
        # get resources
92
        r = client.get(u('resources'))
93
        self.assertEqual(r.status_code, 200)
94
        body = json.loads(r.content)
95
        for name in resource_names:
96
            assertIn(name, body)
97

    
98
        # get quota
99
        r = client.get(u('quotas'))
100
        self.assertEqual(r.status_code, 401)
101

    
102
        headers = {'HTTP_X_AUTH_TOKEN': user.auth_token}
103
        r = client.get(u('quotas/'), **headers)
104
        self.assertEqual(r.status_code, 200)
105
        body = json.loads(r.content)
106
        system_quota = body['system']
107
        assertIn('system', body)
108
        for name in resource_names:
109
            assertIn(name, system_quota)
110

    
111
        r = client.get(u('service_quotas'))
112
        self.assertEqual(r.status_code, 401)
113

    
114
        s1_headers = {'HTTP_X_AUTH_TOKEN': component1.auth_token}
115
        r = client.get(u('service_quotas'), **s1_headers)
116
        self.assertEqual(r.status_code, 200)
117
        body = json.loads(r.content)
118
        assertIn(user.uuid, body)
119

    
120
        r = client.get(u('commissions'), **s1_headers)
121
        self.assertEqual(r.status_code, 200)
122
        body = json.loads(r.content)
123
        self.assertEqual(body, [])
124

    
125
        # issue some commissions
126
        commission_request = {
127
            "force": False,
128
            "auto_accept": False,
129
            "name": "my commission",
130
            "provisions": [
131
                {
132
                    "holder": user.uuid,
133
                    "source": "system",
134
                    "resource": resource11['name'],
135
                    "quantity": 1
136
                },
137
                {
138
                    "holder": user.uuid,
139
                    "source": "system",
140
                    "resource": resource12['name'],
141
                    "quantity": 30000
142
                }]}
143

    
144
        post_data = json.dumps(commission_request)
145
        r = client.post(u('commissions'), post_data,
146
                        content_type='application/json', **s1_headers)
147
        self.assertEqual(r.status_code, 413)
148

    
149
        commission_request = {
150
            "force": False,
151
            "auto_accept": False,
152
            "name": "my commission",
153
            "provisions": [
154
                {
155
                    "holder": user.uuid,
156
                    "source": "system",
157
                    "resource": resource11['name'],
158
                    "quantity": 1
159
                },
160
                {
161
                    "holder": user.uuid,
162
                    "source": "system",
163
                    "resource": resource12['name'],
164
                    "quantity": 100
165
                }]}
166

    
167
        post_data = json.dumps(commission_request)
168
        r = client.post(u('commissions'), post_data,
169
                        content_type='application/json', **s1_headers)
170
        self.assertEqual(r.status_code, 201)
171
        body = json.loads(r.content)
172
        serial = body['serial']
173
        self.assertEqual(serial, 1)
174

    
175
        post_data = json.dumps(commission_request)
176
        r = client.post(u('commissions'), post_data,
177
                        content_type='application/json', **s1_headers)
178
        self.assertEqual(r.status_code, 201)
179
        body = json.loads(r.content)
180
        self.assertEqual(body['serial'], 2)
181

    
182
        post_data = json.dumps(commission_request)
183
        r = client.post(u('commissions'), post_data,
184
                        content_type='application/json', **s1_headers)
185
        self.assertEqual(r.status_code, 201)
186
        body = json.loads(r.content)
187
        self.assertEqual(body['serial'], 3)
188

    
189
        r = client.get(u('commissions'), **s1_headers)
190
        self.assertEqual(r.status_code, 200)
191
        body = json.loads(r.content)
192
        self.assertEqual(body, [1, 2, 3])
193

    
194
        r = client.get(u('commissions/' + str(serial)), **s1_headers)
195
        self.assertEqual(r.status_code, 200)
196
        body = json.loads(r.content)
197
        self.assertEqual(body['serial'], serial)
198
        assertIn('issue_time', body)
199
        provisions = sorted(body['provisions'], key=lambda p: p['resource'])
200
        self.assertEqual(provisions, commission_request['provisions'])
201
        self.assertEqual(body['name'], commission_request['name'])
202

    
203
        r = client.get(u('service_quotas?user=' + user.uuid), **s1_headers)
204
        self.assertEqual(r.status_code, 200)
205
        body = json.loads(r.content)
206
        user_quota = body[user.uuid]
207
        system_quota = user_quota['system']
208
        r11 = system_quota[resource11['name']]
209
        self.assertEqual(r11['usage'], 3)
210
        self.assertEqual(r11['pending'], 3)
211

    
212
        # resolve pending commissions
213
        resolve_data = {
214
            "accept": [1, 3],
215
            "reject": [2, 3, 4],
216
        }
217
        post_data = json.dumps(resolve_data)
218

    
219
        r = client.post(u('commissions/action'), post_data,
220
                        content_type='application/json', **s1_headers)
221
        self.assertEqual(r.status_code, 200)
222
        body = json.loads(r.content)
223
        self.assertEqual(body['accepted'], [1])
224
        self.assertEqual(body['rejected'], [2])
225
        failed = body['failed']
226
        self.assertEqual(len(failed), 2)
227

    
228
        r = client.get(u('commissions/' + str(serial)), **s1_headers)
229
        self.assertEqual(r.status_code, 404)
230

    
231
        # auto accept
232
        commission_request = {
233
            "auto_accept": True,
234
            "name": "my commission",
235
            "provisions": [
236
                {
237
                    "holder": user.uuid,
238
                    "source": "system",
239
                    "resource": resource11['name'],
240
                    "quantity": 1
241
                },
242
                {
243
                    "holder": user.uuid,
244
                    "source": "system",
245
                    "resource": resource12['name'],
246
                    "quantity": 100
247
                }]}
248

    
249
        post_data = json.dumps(commission_request)
250
        r = client.post(u('commissions'), post_data,
251
                        content_type='application/json', **s1_headers)
252
        self.assertEqual(r.status_code, 201)
253
        body = json.loads(r.content)
254
        serial = body['serial']
255
        self.assertEqual(serial, 4)
256

    
257
        r = client.get(u('commissions/' + str(serial)), **s1_headers)
258
        self.assertEqual(r.status_code, 404)
259

    
260
        # malformed
261
        commission_request = {
262
            "auto_accept": True,
263
            "name": "my commission",
264
            "provisions": [
265
                {
266
                    "holder": user.uuid,
267
                    "source": "system",
268
                    "resource": resource11['name'],
269
                }
270
            ]}
271

    
272
        post_data = json.dumps(commission_request)
273
        r = client.post(u('commissions'), post_data,
274
                        content_type='application/json', **s1_headers)
275
        self.assertEqual(r.status_code, 400)
276

    
277
        commission_request = {
278
            "auto_accept": True,
279
            "name": "my commission",
280
            "provisions": "dummy"}
281

    
282
        post_data = json.dumps(commission_request)
283
        r = client.post(u('commissions'), post_data,
284
                        content_type='application/json', **s1_headers)
285
        self.assertEqual(r.status_code, 400)
286

    
287
        r = client.post(u('commissions'), commission_request,
288
                        content_type='application/json', **s1_headers)
289
        self.assertEqual(r.status_code, 400)
290

    
291
        # no holding
292
        commission_request = {
293
            "auto_accept": True,
294
            "name": "my commission",
295
            "provisions": [
296
                {
297
                    "holder": user.uuid,
298
                    "source": "system",
299
                    "resource": "non existent",
300
                    "quantity": 1
301
                },
302
                {
303
                    "holder": user.uuid,
304
                    "source": "system",
305
                    "resource": resource12['name'],
306
                    "quantity": 100
307
                }]}
308

    
309
        post_data = json.dumps(commission_request)
310
        r = client.post(u('commissions'), post_data,
311
                        content_type='application/json', **s1_headers)
312
        self.assertEqual(r.status_code, 404)
313

    
314
        # release
315
        commission_request = {
316
            "provisions": [
317
                {
318
                    "holder": user.uuid,
319
                    "source": "system",
320
                    "resource": resource11['name'],
321
                    "quantity": -1
322
                }
323
            ]}
324

    
325
        post_data = json.dumps(commission_request)
326
        r = client.post(u('commissions'), post_data,
327
                        content_type='application/json', **s1_headers)
328
        self.assertEqual(r.status_code, 201)
329
        body = json.loads(r.content)
330
        serial = body['serial']
331

    
332
        accept_data = {'accept': ""}
333
        post_data = json.dumps(accept_data)
334
        r = client.post(u('commissions/' + str(serial) + '/action'), post_data,
335
                        content_type='application/json', **s1_headers)
336
        self.assertEqual(r.status_code, 200)
337

    
338
        reject_data = {'reject': ""}
339
        post_data = json.dumps(accept_data)
340
        r = client.post(u('commissions/' + str(serial) + '/action'), post_data,
341
                        content_type='application/json', **s1_headers)
342
        self.assertEqual(r.status_code, 404)
343

    
344
        # force
345
        commission_request = {
346
            "force": True,
347
            "provisions": [
348
                {
349
                    "holder": user.uuid,
350
                    "source": "system",
351
                    "resource": resource11['name'],
352
                    "quantity": 100
353
                }]}
354

    
355
        post_data = json.dumps(commission_request)
356
        r = client.post(u('commissions'), post_data,
357
                        content_type='application/json', **s1_headers)
358
        self.assertEqual(r.status_code, 201)
359

    
360
        commission_request = {
361
            "force": True,
362
            "provisions": [
363
                {
364
                    "holder": user.uuid,
365
                    "source": "system",
366
                    "resource": resource11['name'],
367
                    "quantity": -200
368
                }]}
369

    
370
        post_data = json.dumps(commission_request)
371
        r = client.post(u('commissions'), post_data,
372
                        content_type='application/json', **s1_headers)
373
        self.assertEqual(r.status_code, 413)
374

    
375
        r = client.get(u('quotas'), **headers)
376
        self.assertEqual(r.status_code, 200)
377
        body = json.loads(r.content)
378
        system_quota = body['system']
379
        r11 = system_quota[resource11['name']]
380
        self.assertEqual(r11['usage'], 102)
381
        self.assertEqual(r11['pending'], 101)
382

    
383

    
384
class TokensApiTest(TestCase):
385
    def setUp(self):
386
        backend = activation_backends.get_backend()
387

    
388
        self.user1 = AstakosUser.objects.create(
389
            email='test1', email_verified=True, moderated=True,
390
            is_rejected=False)
391
        backend.activate_user(self.user1)
392
        assert self.user1.is_active is True
393

    
394
        self.user2 = AstakosUser.objects.create(
395
            email='test2', email_verified=True, moderated=True,
396
            is_rejected=False)
397
        backend.activate_user(self.user2)
398
        assert self.user2.is_active is True
399

    
400
        c1 = Component(name='component1', url='http://localhost/component1')
401
        c1.save()
402
        s1 = Service(component=c1, type='type1', name='service1')
403
        s1.save()
404
        e1 = Endpoint(service=s1)
405
        e1.save()
406
        e1.data.create(key='versionId', value='v1.0')
407
        e1.data.create(key='publicURL', value='http://localhost:8000/s1/v1.0')
408

    
409
        s2 = Service(component=c1, type='type2', name='service2')
410
        s2.save()
411
        e2 = Endpoint(service=s2)
412
        e2.save()
413
        e2.data.create(key='versionId', value='v1.0')
414
        e2.data.create(key='publicURL', value='http://localhost:8000/s2/v1.0')
415

    
416
        c2 = Component(name='component2', url='http://localhost/component2')
417
        c2.save()
418
        s3 = Service(component=c2, type='type3', name='service3')
419
        s3.save()
420
        e3 = Endpoint(service=s3)
421
        e3.save()
422
        e3.data.create(key='versionId', value='v2.0')
423
        e3.data.create(key='publicURL', value='http://localhost:8000/s3/v2.0')
424

    
425
    def test_authenticate(self):
426
        client = Client()
427
        url = reverse('astakos.api.tokens.authenticate')
428

    
429
        # Check not allowed method
430
        r = client.get(url, post_data={})
431
        self.assertEqual(r.status_code, 400)
432

    
433
        # check public mode
434
        r = client.post(url, CONTENT_LENGTH=0)
435
        self.assertEqual(r.status_code, 200)
436
        self.assertTrue(r['Content-Type'].startswith('application/json'))
437
        try:
438
            body = json.loads(r.content)
439
        except Exception, e:
440
            self.fail(e)
441
        self.assertTrue('token' not in body.get('access'))
442
        self.assertTrue('user' not in body.get('access'))
443
        self.assertTrue('serviceCatalog' in body.get('access'))
444

    
445
        # Check unsupported xml input
446
        post_data = """
447
            <?xml version="1.0" encoding="UTF-8"?>
448
                <auth xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
449
                 xmlns="http://docs.openstack.org/identity/api/v2.0"
450
                 tenantName="%s">
451
                  <passwordCredentials username="%s" password="%s"/>
452
                </auth>""" % (self.user1.uuid, self.user1.uuid,
453
                              self.user1.auth_token)
454
        r = client.post(url, post_data, content_type='application/xml')
455
        self.assertEqual(r.status_code, 400)
456
        body = json.loads(r.content)
457
        self.assertEqual(body['badRequest']['message'],
458
                         "Unsupported Content-type: 'application/xml'")
459

    
460
        # Check malformed request: missing password
461
        post_data = """{"auth":{"passwordCredentials":{"username":"%s"},
462
                                "tenantName":"%s"}}""" % (
463
            self.user1.uuid, self.user1.uuid)
464
        r = client.post(url, post_data, content_type='application/json')
465
        self.assertEqual(r.status_code, 400)
466
        body = json.loads(r.content)
467
        self.assertTrue(body['badRequest']['message'].
468
                        startswith('Malformed request'))
469

    
470
        # Check malformed request: missing username
471
        post_data = """{"auth":{"passwordCredentials":{"password":"%s"},
472
                                "tenantName":"%s"}}""" % (
473
            self.user1.auth_token, self.user1.uuid)
474
        r = client.post(url, post_data, content_type='application/json')
475
        self.assertEqual(r.status_code, 400)
476
        body = json.loads(r.content)
477
        self.assertTrue(body['badRequest']['message'].
478
                        startswith('Malformed request'))
479

    
480
        # Check invalid pass
481
        post_data = """{"auth":{"passwordCredentials":{"username":"%s",
482
                                                       "password":"%s"},
483
                                "tenantName":"%s"}}""" % (
484
            self.user1.uuid, '', self.user1.uuid)
485
        r = client.post(url, post_data, content_type='application/json')
486
        self.assertEqual(r.status_code, 401)
487
        body = json.loads(r.content)
488
        self.assertEqual(body['unauthorized']['message'],
489
                         'Invalid token')
490

    
491
        # Check inconsistent pass
492
        post_data = """{"auth":{"passwordCredentials":{"username":"%s",
493
                                                       "password":"%s"},
494
                                "tenantName":"%s"}}""" % (
495
            self.user1.uuid, self.user2.auth_token, self.user2.uuid)
496
        r = client.post(url, post_data, content_type='application/json')
497
        self.assertEqual(r.status_code, 401)
498
        body = json.loads(r.content)
499
        self.assertEqual(body['unauthorized']['message'],
500
                         'Invalid credentials')
501

    
502
        # Check invalid json data
503
        r = client.post(url, "not json", content_type='application/json')
504
        self.assertEqual(r.status_code, 400)
505
        body = json.loads(r.content)
506
        self.assertEqual(body['badRequest']['message'], 'Invalid JSON data')
507

    
508
        # Check auth with token
509
        post_data = """{"auth":{"token": {"id":"%s"},
510
                        "tenantName":"%s"}}""" % (
511
            self.user1.auth_token, self.user1.uuid)
512
        r = client.post(url, post_data, content_type='application/json')
513
        self.assertEqual(r.status_code, 200)
514
        self.assertTrue(r['Content-Type'].startswith('application/json'))
515
        try:
516
            body = json.loads(r.content)
517
        except Exception, e:
518
            self.fail(e)
519

    
520
        # Check malformed request: missing token
521
        post_data = """{"auth":{"auth_token":{"id":"%s"},
522
                                "tenantName":"%s"}}""" % (
523
            self.user1.auth_token, self.user1.uuid)
524
        r = client.post(url, post_data, content_type='application/json')
525
        self.assertEqual(r.status_code, 400)
526
        body = json.loads(r.content)
527
        self.assertTrue(body['badRequest']['message'].
528
                        startswith('Malformed request'))
529

    
530
        # Check bad request: inconsistent tenant
531
        post_data = """{"auth":{"token":{"id":"%s"},
532
                                "tenantName":"%s"}}""" % (
533
            self.user1.auth_token, self.user2.uuid)
534
        r = client.post(url, post_data, content_type='application/json')
535
        self.assertEqual(r.status_code, 400)
536
        body = json.loads(r.content)
537
        self.assertEqual(body['badRequest']['message'],
538
                         'Not conforming tenantName')
539

    
540
        # Check bad request: inconsistent tenant
541
        post_data = """{"auth":{"token":{"id":"%s"},
542
                                "tenantName":""}}""" % (
543
            self.user1.auth_token)
544
        r = client.post(url, post_data, content_type='application/json')
545
        self.assertEqual(r.status_code, 200)
546

    
547
        # Check successful json response
548
        post_data = """{"auth":{"passwordCredentials":{"username":"%s",
549
                                                       "password":"%s"},
550
                                "tenantName":"%s"}}""" % (
551
            self.user1.uuid, self.user1.auth_token, self.user1.uuid)
552
        r = client.post(url, post_data, content_type='application/json')
553
        self.assertEqual(r.status_code, 200)
554
        self.assertTrue(r['Content-Type'].startswith('application/json'))
555
        try:
556
            body = json.loads(r.content)
557
        except Exception, e:
558
            self.fail(e)
559

    
560
        try:
561
            token = body['access']['token']['id']
562
            user = body['access']['user']['id']
563
            service_catalog = body['access']['serviceCatalog']
564
        except KeyError:
565
            self.fail('Invalid response')
566

    
567
        self.assertEqual(token, self.user1.auth_token)
568
        self.assertEqual(user, self.user1.uuid)
569
        self.assertEqual(len(service_catalog), 3)
570

    
571
        # Check successful xml response
572
        headers = {'HTTP_ACCEPT': 'application/xml'}
573
        post_data = """{"auth":{"passwordCredentials":{"username":"%s",
574
                                                       "password":"%s"},
575
                                "tenantName":"%s"}}""" % (
576
            self.user1.uuid, self.user1.auth_token, self.user1.uuid)
577
        r = client.post(url, post_data, content_type='application/json',
578
                        **headers)
579
        self.assertEqual(r.status_code, 200)
580
        self.assertTrue(r['Content-Type'].startswith('application/xml'))
581
#        try:
582
#            body = minidom.parseString(r.content)
583
#        except Exception, e:
584
#            self.fail(e)
585

    
586

    
587
class WrongPathAPITest(TestCase):
588
    def test_catch_wrong_account_paths(self, *args):
589
        path = get_service_path(astakos_services, 'account', 'v1.0')
590
        path = join_urls(BASE_HOST, path, 'nonexistent')
591
        response = self.client.get(path)
592
        self.assertEqual(response.status_code, 400)
593
        try:
594
            error = json.loads(response.content)
595
        except ValueError:
596
            self.assertTrue(False)
597

    
598
    def test_catch_wrong_identity_paths(self, *args):
599
        path = get_service_path(astakos_services, 'identity', 'v2.0')
600
        path = join_urls(BASE_HOST, path, 'nonexistent')
601
        response = self.client.get(path)
602
        self.assertEqual(response.status_code, 400)
603
        try:
604
            json.loads(response.content)
605
        except ValueError:
606
            self.assertTrue(False)