Revision c80722ce

b/snf-astakos-app/astakos/im/tests.py
42 42
from django.core import mail
43 43
from django.http import SimpleCookie, HttpRequest, QueryDict
44 44
from django.utils.importlib import import_module
45
from django.utils import simplejson as json
45 46

  
46 47
from astakos.im.activation_backends import *
47 48
from astakos.im.target.shibboleth import Tokens as ShibbolethTokens
......
56 57
from astakos.im import messages
57 58
from astakos.im import auth_providers
58 59
from astakos.im import quotas
60
from astakos.im import resources
59 61

  
60 62
from django.conf import settings
61 63

  
......
1390 1392
        newlimit = user_quotas[self.member.uuid]['system'][resource]['limit']
1391 1393
        # 200 - 100 from project
1392 1394
        self.assertEqual(newlimit, 100)
1395

  
1396

  
1397
ROOT = '/astakos/api/'
1398
u = lambda url: ROOT + url
1399

  
1400

  
1401
class QuotaAPITest(TestCase):
1402
    def test_0(self):
1403
        client = Client()
1404
        # custom service resources
1405
        service1 = Service.objects.create(
1406
            name="service1", api_url="http://service1.api")
1407
        resource11 = {"name": "service1.resource11",
1408
                      "desc": "resource11 desc",
1409
                      "allow_in_projects": True}
1410
        r, _ = resources.add_resource(service1, resource11)
1411
        resources.update_resource(r, 100)
1412
        resource12 = {"name": "service1.resource12",
1413
                      "desc": "resource11 desc",
1414
                      "unit": "bytes"}
1415
        r, _ = resources.add_resource(service1, resource12)
1416
        resources.update_resource(r, 1024)
1417

  
1418
        # create user
1419
        user = get_local_user('test@grnet.gr')
1420
        quotas.qh_sync_user(user.id)
1421

  
1422
        # create another service
1423
        service2 = Service.objects.create(
1424
            name="service2", api_url="http://service2.api")
1425
        resource21 = {"name": "service2.resource21",
1426
                      "desc": "resource11 desc",
1427
                      "allow_in_projects": False}
1428
        r, _ = resources.add_resource(service2, resource21)
1429
        resources.update_resource(r, 3)
1430

  
1431
        resource_names = [r['name'] for r in
1432
                          [resource11, resource12, resource21]]
1433

  
1434
        # get resources
1435
        r = client.get(u('resources'), follow=True)
1436
        self.assertEqual(r.status_code, 200)
1437
        body = json.loads(r.content)
1438
        for name in resource_names:
1439
            self.assertIn(name, body)
1440

  
1441
        # get quota
1442
        r = client.get(u('quotas'), follow=True)
1443
        self.assertEqual(r.status_code, 401)
1444

  
1445
        headers = {'HTTP_X_AUTH_TOKEN': user.auth_token}
1446
        r = client.get(u('quotas'), follow=True, **headers)
1447
        self.assertEqual(r.status_code, 200)
1448
        body = json.loads(r.content)
1449
        system_quota = body['system']
1450
        self.assertIn('system', body)
1451
        for name in resource_names:
1452
            self.assertIn(name, system_quota)
1453

  
1454
        r = client.get(u('service_quotas'), follow=True)
1455
        self.assertEqual(r.status_code, 401)
1456

  
1457
        s1_headers = {'HTTP_X_AUTH_TOKEN': service1.auth_token}
1458
        r = client.get(u('service_quotas'), follow=True, **s1_headers)
1459
        self.assertEqual(r.status_code, 200)
1460
        body = json.loads(r.content)
1461
        self.assertIn(user.uuid, body)
1462

  
1463
        r = client.get(u('commissions'), follow=True, **s1_headers)
1464
        self.assertEqual(r.status_code, 200)
1465
        body = json.loads(r.content)
1466
        self.assertEqual(body, [])
1467

  
1468
        # issue some commissions
1469
        commission_request = {
1470
            "force": False,
1471
            "auto_accept": False,
1472
            "name": "my commission",
1473
            "provisions": [
1474
                {
1475
                    "holder": user.uuid,
1476
                    "source": "system",
1477
                    "resource": resource11['name'],
1478
                    "quantity": 1
1479
                },
1480
                {
1481
                    "holder": user.uuid,
1482
                    "source": "system",
1483
                    "resource": resource12['name'],
1484
                    "quantity": 30000
1485
                }]}
1486

  
1487
        post_data = json.dumps(commission_request)
1488
        r = client.post(u('commissions'), post_data,
1489
                        content_type='application/json', **s1_headers)
1490
        self.assertEqual(r.status_code, 413)
1491

  
1492
        commission_request = {
1493
            "force": False,
1494
            "auto_accept": False,
1495
            "name": "my commission",
1496
            "provisions": [
1497
                {
1498
                    "holder": user.uuid,
1499
                    "source": "system",
1500
                    "resource": resource11['name'],
1501
                    "quantity": 1
1502
                },
1503
                {
1504
                    "holder": user.uuid,
1505
                    "source": "system",
1506
                    "resource": resource12['name'],
1507
                    "quantity": 100
1508
                }]}
1509

  
1510
        post_data = json.dumps(commission_request)
1511
        r = client.post(u('commissions'), post_data,
1512
                        content_type='application/json', **s1_headers)
1513
        self.assertEqual(r.status_code, 201)
1514
        body = json.loads(r.content)
1515
        serial = body['serial']
1516
        self.assertEqual(serial, 1)
1517

  
1518
        post_data = json.dumps(commission_request)
1519
        r = client.post(u('commissions'), post_data,
1520
                        content_type='application/json', **s1_headers)
1521
        self.assertEqual(r.status_code, 201)
1522
        body = json.loads(r.content)
1523
        self.assertEqual(body['serial'], 2)
1524

  
1525
        post_data = json.dumps(commission_request)
1526
        r = client.post(u('commissions'), post_data,
1527
                        content_type='application/json', **s1_headers)
1528
        self.assertEqual(r.status_code, 201)
1529
        body = json.loads(r.content)
1530
        self.assertEqual(body['serial'], 3)
1531

  
1532
        r = client.get(u('commissions'), follow=True, **s1_headers)
1533
        self.assertEqual(r.status_code, 200)
1534
        body = json.loads(r.content)
1535
        self.assertEqual(body, [1, 2, 3])
1536

  
1537
        r = client.get(u('commissions/' + str(serial)), follow=True,
1538
                       **s1_headers)
1539
        self.assertEqual(r.status_code, 200)
1540
        body = json.loads(r.content)
1541
        self.assertEqual(body['serial'], serial)
1542
        self.assertIn('issue_time', body)
1543
        self.assertEqual(body['provisions'], commission_request['provisions'])
1544
        self.assertEqual(body['name'], commission_request['name'])
1545

  
1546
        r = client.get(u('service_quotas?user=' + user.uuid),
1547
                       follow=True, **s1_headers)
1548
        self.assertEqual(r.status_code, 200)
1549
        body = json.loads(r.content)
1550
        user_quota = body[user.uuid]
1551
        system_quota = user_quota['system']
1552
        r11 = system_quota[resource11['name']]
1553
        self.assertEqual(r11['usage'], 3)
1554
        self.assertEqual(r11['pending'], 3)
1555

  
1556
        # resolve pending commissions
1557
        resolve_data = {
1558
            "accept": [1, 3],
1559
            "reject": [2, 3, 4],
1560
        }
1561
        post_data = json.dumps(resolve_data)
1562

  
1563
        r = client.post(u('commissions/action'), post_data,
1564
                        content_type='application/json', **s1_headers)
1565
        self.assertEqual(r.status_code, 200)
1566
        body = json.loads(r.content)
1567
        self.assertEqual(body['accepted'], [1])
1568
        self.assertEqual(body['rejected'], [2])
1569
        failed = body['failed']
1570
        self.assertEqual(len(failed), 2)
1571

  
1572
        r = client.get(u('commissions/' + str(serial)), follow=True,
1573
                       **s1_headers)
1574
        self.assertEqual(r.status_code, 404)
1575

  
1576
        # auto accept
1577
        commission_request = {
1578
            "auto_accept": True,
1579
            "name": "my commission",
1580
            "provisions": [
1581
                {
1582
                    "holder": user.uuid,
1583
                    "source": "system",
1584
                    "resource": resource11['name'],
1585
                    "quantity": 1
1586
                },
1587
                {
1588
                    "holder": user.uuid,
1589
                    "source": "system",
1590
                    "resource": resource12['name'],
1591
                    "quantity": 100
1592
                }]}
1593

  
1594
        post_data = json.dumps(commission_request)
1595
        r = client.post(u('commissions'), post_data,
1596
                        content_type='application/json', **s1_headers)
1597
        self.assertEqual(r.status_code, 201)
1598
        body = json.loads(r.content)
1599
        serial = body['serial']
1600
        self.assertEqual(serial, 4)
1601

  
1602
        r = client.get(u('commissions/' + str(serial)), follow=True,
1603
                       **s1_headers)
1604
        self.assertEqual(r.status_code, 404)
1605

  
1606
        # malformed
1607
        commission_request = {
1608
            "auto_accept": True,
1609
            "name": "my commission",
1610
            "provisions": [
1611
                {
1612
                    "holder": user.uuid,
1613
                    "source": "system",
1614
                    "resource": resource11['name'],
1615
                }
1616
            ]}
1617

  
1618
        post_data = json.dumps(commission_request)
1619
        r = client.post(u('commissions'), post_data,
1620
                        content_type='application/json', **s1_headers)
1621
        self.assertEqual(r.status_code, 400)
1622

  
1623
        commission_request = {
1624
            "auto_accept": True,
1625
            "name": "my commission",
1626
            "provisions": "dummy"}
1627

  
1628
        post_data = json.dumps(commission_request)
1629
        r = client.post(u('commissions'), post_data,
1630
                        content_type='application/json', **s1_headers)
1631
        self.assertEqual(r.status_code, 400)
1632

  
1633
        r = client.post(u('commissions'), commission_request,
1634
                        content_type='application/json', **s1_headers)
1635
        self.assertEqual(r.status_code, 400)
1636

  
1637
        # no holding
1638
        commission_request = {
1639
            "auto_accept": True,
1640
            "name": "my commission",
1641
            "provisions": [
1642
                {
1643
                    "holder": user.uuid,
1644
                    "source": "system",
1645
                    "resource": "non existent",
1646
                    "quantity": 1
1647
                },
1648
                {
1649
                    "holder": user.uuid,
1650
                    "source": "system",
1651
                    "resource": resource12['name'],
1652
                    "quantity": 100
1653
                }]}
1654

  
1655
        post_data = json.dumps(commission_request)
1656
        r = client.post(u('commissions'), post_data,
1657
                        content_type='application/json', **s1_headers)
1658
        self.assertEqual(r.status_code, 404)
1659

  
1660
        # release
1661
        commission_request = {
1662
            "provisions": [
1663
                {
1664
                    "holder": user.uuid,
1665
                    "source": "system",
1666
                    "resource": resource11['name'],
1667
                    "quantity": -1
1668
                }
1669
            ]}
1670

  
1671
        post_data = json.dumps(commission_request)
1672
        r = client.post(u('commissions'), post_data,
1673
                        content_type='application/json', **s1_headers)
1674
        self.assertEqual(r.status_code, 201)
1675
        body = json.loads(r.content)
1676
        serial = body['serial']
1677

  
1678
        accept_data = {'accept': ""}
1679
        post_data = json.dumps(accept_data)
1680
        r = client.post(u('commissions/' + str(serial) + '/action'), post_data,
1681
                        content_type='application/json', **s1_headers)
1682
        self.assertEqual(r.status_code, 200)
1683

  
1684
        reject_data = {'reject': ""}
1685
        post_data = json.dumps(accept_data)
1686
        r = client.post(u('commissions/' + str(serial) + '/action'), post_data,
1687
                        content_type='application/json', **s1_headers)
1688
        self.assertEqual(r.status_code, 404)
1689

  
1690
        # force
1691
        commission_request = {
1692
            "force": True,
1693
            "provisions": [
1694
                {
1695
                    "holder": user.uuid,
1696
                    "source": "system",
1697
                    "resource": resource11['name'],
1698
                    "quantity": 100
1699
                }]}
1700

  
1701
        post_data = json.dumps(commission_request)
1702
        r = client.post(u('commissions'), post_data,
1703
                        content_type='application/json', **s1_headers)
1704
        self.assertEqual(r.status_code, 201)
1705
        body = json.loads(r.content)
1706

  
1707
        r = client.get(u('quotas'), **headers)
1708
        self.assertEqual(r.status_code, 200)
1709
        body = json.loads(r.content)
1710
        system_quota = body['system']
1711
        r11 = system_quota[resource11['name']]
1712
        self.assertEqual(r11['usage'], 102)
1713
        self.assertEqual(r11['pending'], 101)

Also available in: Unified diff