@@ -1479,6 +1479,167 @@ def test_LoginApiDomain(self):
1479
1479
return
1480
1480
1481
1481
1482
+ class TestUserAPIKeys (cloudstackTestCase ):
1483
+
1484
+ @classmethod
1485
+ def setUpClass (cls ):
1486
+ cls .testClient = super (TestUserAPIKeys , cls ).getClsTestClient ()
1487
+ cls .api_client = cls .testClient .getApiClient ()
1488
+
1489
+ cls .services = Services ().services
1490
+ cls .domain = get_domain (cls .api_client )
1491
+ cls .zone = get_zone (cls .api_client , cls .testClient .getZoneForTests ())
1492
+ cls .services ['mode' ] = cls .zone .networktype
1493
+ # Create an account, domain etc
1494
+ cls .domain = Domain .create (
1495
+ cls .api_client ,
1496
+ cls .services ["domain" ],
1497
+ )
1498
+ cls .account = Account .create (
1499
+ cls .api_client ,
1500
+ cls .services ["account" ],
1501
+ admin = False ,
1502
+ domainid = cls .domain .id
1503
+ )
1504
+ cls .domain_2 = Domain .create (
1505
+ cls .api_client ,
1506
+ cls .services ["domain" ],
1507
+ )
1508
+ cls .account_2 = Account .create (
1509
+ cls .api_client ,
1510
+ cls .services ["account" ],
1511
+ admin = False ,
1512
+ domainid = cls .domain_2 .id
1513
+ )
1514
+ cls ._cleanup = [
1515
+ cls .account ,
1516
+ cls .domain ,
1517
+ cls .account_2 ,
1518
+ cls .domain_2
1519
+ ]
1520
+ return
1521
+
1522
+ @classmethod
1523
+ def tearDownClass (cls ):
1524
+ try :
1525
+ # Cleanup resources used
1526
+ cleanup_resources (cls .api_client , cls ._cleanup )
1527
+ except Exception as e :
1528
+ raise Exception ("Warning: Exception during cleanup : %s" % e )
1529
+ return
1530
+
1531
+ def setUp (self ):
1532
+ self .apiclient = self .testClient .getApiClient ()
1533
+ self .dbclient = self .testClient .getDbConnection ()
1534
+ self .cleanup = []
1535
+ return
1536
+
1537
+ def tearDown (self ):
1538
+ try :
1539
+ # Clean up, terminate the created network offerings
1540
+ cleanup_resources (self .apiclient , self .cleanup )
1541
+ except Exception as e :
1542
+ raise Exception ("Warning: Exception during cleanup : %s" % e )
1543
+ return
1544
+
1545
+ @attr (tags = [
1546
+ "role" ,
1547
+ "accounts" ,
1548
+ "simulator" ,
1549
+ "advanced" ,
1550
+ "advancedns" ,
1551
+ "basic" ,
1552
+ "eip" ,
1553
+ "sg"
1554
+ ])
1555
+ def test_user_key_renew_same_account (self ):
1556
+ # Create an User associated with the account
1557
+ user_1 = User .create (
1558
+ self .apiclient ,
1559
+ self .services ["user" ],
1560
+ account = self .account .name ,
1561
+ domainid = self .domain .id
1562
+ )
1563
+ self .cleanup .append (user_1 )
1564
+ account_response = list_accounts (
1565
+ self .apiclient ,
1566
+ id = self .account .id
1567
+ )[0 ]
1568
+ self .assertEqual (
1569
+ hasattr (account_response , 'user' ),
1570
+ True ,
1571
+ "Users are included in account response" )
1572
+
1573
+ account_users = account_response .user
1574
+ self .assertEqual (
1575
+ isinstance (account_users , list ),
1576
+ True ,
1577
+ "Check account for valid data"
1578
+ )
1579
+ self .assertNotEqual (
1580
+ len (account_users ),
1581
+ 0 ,
1582
+ "Check number of User in Account" )
1583
+ [user ] = [u for u in account_users if u .username == user_1 .username ]
1584
+ self .assertEqual (
1585
+ user .apikey ,
1586
+ None ,
1587
+ "Check that the user don't have an API key yet" )
1588
+
1589
+ self .debug ("Register API keys for user" )
1590
+ userkeys = User .registerUserKeys (self .apiclient , user_1 .id )
1591
+ users = list_accounts (
1592
+ self .apiclient ,
1593
+ id = self .account .id
1594
+ )[0 ].user
1595
+ [user ] = [u for u in users if u .id == user_1 .id ]
1596
+ self .assertEqual (
1597
+ user .apikey ,
1598
+ userkeys .apikey ,
1599
+ "Check User api key" )
1600
+ self .assertEqual (
1601
+ user .secretkey ,
1602
+ userkeys .secretkey ,
1603
+ "Check User having secret key" )
1604
+
1605
+ self .debug ("Get test client with user keys" )
1606
+ cs_api = self .testClient .getUserApiClient (
1607
+ UserName = self .account .name ,
1608
+ DomainName = self .account .domain )
1609
+ self .debug ("Renew API keys for user using current keys" )
1610
+ new_keys = User .registerUserKeys (cs_api , user_1 .id )
1611
+ self .assertNotEqual (
1612
+ userkeys .apikey ,
1613
+ new_keys .apikey ,
1614
+ "Check API key is different" )
1615
+ self .assertNotEqual (
1616
+ userkeys .secretkey ,
1617
+ new_keys .secretkey ,
1618
+ "Check secret key is different" )
1619
+
1620
+ @attr (tags = [
1621
+ "role" ,
1622
+ "accounts" ,
1623
+ "simulator" ,
1624
+ "advanced" ,
1625
+ "advancedns" ,
1626
+ "basic" ,
1627
+ "eip" ,
1628
+ "sg"
1629
+ ])
1630
+ def test_user_cannot_renew_other_keys (self ):
1631
+ cs_api = self .testClient .getUserApiClient (
1632
+ UserName = self .account .name ,
1633
+ DomainName = self .account .domain )
1634
+ self .debug ("Try to change API key of an account in another domain" )
1635
+ users = list_accounts (
1636
+ self .apiclient ,
1637
+ id = self .account_2 .id
1638
+ )[0 ].user
1639
+ with self .assertRaises (CloudstackAPIException ) as e :
1640
+ User .registerUserKeys (cs_api , users [0 ].id )
1641
+
1642
+
1482
1643
class TestDomainForceRemove (cloudstackTestCase ):
1483
1644
1484
1645
@classmethod
0 commit comments