# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import unittest

from azure.cli.testsdk import ScenarioTest, ResourceGroupPreparer


class ResourceLockTests(ScenarioTest):
    def test_list_locks(self):
        # just make sure this doesn't throw
        self.cmd('az lock list').get_output_in_json()

    # Test for subscription level locks
    def test_lock_create_list_delete(self):
        for lock_type in ['ReadOnly', 'CanNotDelete']:
            self.cmd('az lock create -n foo --lock-type {}'.format(lock_type))
            locks_list = self.cmd('az lock list').get_output_in_json()
            assert locks_list
            assert 'foo' in [l['name'] for l in locks_list]
            lock = self.cmd('az lock show -n foo').get_output_in_json()
            self.assertEqual(lock.get('name', None), 'foo')
            self.assertEqual(lock.get('level', None), lock_type)
            self.cmd('az lock delete -n foo')

    # Test for resource group level locks
    @ResourceGroupPreparer()
    def test_lock_create_list_delete_resource_group(self, resource_group):
        for lock_type in ['ReadOnly', 'CanNotDelete']:
            self.cmd('az lock create -n foo -g {} --lock-type {}'.format(resource_group, lock_type))
            locks_list = self.cmd('az lock list').get_output_in_json()
            assert 'foo' in [l['name'] for l in locks_list]
            assert locks_list
            lock = self.cmd('az lock show -g {} -n foo'.format(resource_group)).get_output_in_json()
            self.assertEqual(lock.get('name', None), 'foo')
            self.assertEqual(lock.get('level', None), lock_type)
            self.cmd('az lock delete -g {} -n foo'.format(resource_group))

    # TODO: test for resource group level locks


if __name__ == '__main__':
    unittest.main()

@@ -803,32 +803,104 @@
 mat(lock_type))%0A
+            self.addCleanup(lambda: self.cmd('az lock delete -n foo'))%0A%0A
             lock
@@ -958,39 +958,48 @@
 n()%0A            
+self.
 assert
- 
+True(
 locks_list%0A     
@@ -984,32 +984,33 @@
 tTrue(locks_list
+)
 %0A            ass
@@ -1002,39 +1002,44 @@
             
+self.
 assert
- 
+In(
 'foo'
- in
+,
  %5Bl%5B'name'%5D 
@@ -1050,32 +1050,34 @@
 l in locks_list%5D
+)%0A
 %0A            loc
@@ -1261,54 +1261,8 @@
 ype)
-%0A            self.cmd('az lock delete -n foo')
 %0A%0A  
@@ -1553,32 +1553,170 @@
 up, lock_type))%0A
+%0A            delete_cmd = 'az lock delete -g %7B%7D -n foo'.format(resource_group)%0A            self.addCleanup(lambda: self.cmd(delete_cmd))%0A%0A
             lock
@@ -1786,44 +1786,24 @@
     
+self.
 assert
- 'foo' in %5Bl%5B'name'%5D for l in 
+True(
 lock
@@ -1808,17 +1808,17 @@
 cks_list
-%5D
+)
 %0A       
@@ -1822,22 +1822,56 @@
         
+self.
 assert
+In('foo', %5Bl%5B'name'%5D for l in
  locks_l
@@ -1873,16 +1873,19 @@
 cks_list
+%5D)%0A
 %0A       
@@ -2106,83 +2106,8 @@
 ype)
-%0A            self.cmd('az lock delete -g %7B%7D -n foo'.format(resource_group))
 %0A%0A  

Make sure locks are removed in automation