class MaasClientTest(unittest.TestCase):
    def setUp(self):
        self.c = MaasClient(AUTH)
        self.tag = 'a-test-tag-' + str(uuid.uuid1())
        res = self.c.tag_new(self.tag)
        self.assertTrue(res)

    def tearDown(self):
        res = self.c.tag_delete(self.tag)
        self.assertTrue(res)

    def test_get_tags(self):
        self.assertTrue(self.tag in [t['name'] for t in self.c.tags])
class MaasClientTest(unittest.TestCase):
    def setUp(self):
        self.c = MaasClient(AUTH)
        self.tag = 'a-test-tag-' + str(uuid.uuid1())
        res = self.c.tag_new(self.tag)
        self.assertTrue(res)

    def tearDown(self):
        res = self.c.tag_delete(self.tag)
        self.assertTrue(res)

    def test_get_tags(self):
        self.assertTrue(self.tag in [t['name'] for t in self.c.tags])
class MaasClientTagTest(unittest.TestCase):
    def setUp(self):
        self.mock_auth = MagicMock()
        self.api_url = 'apiurl'
        url_p = PropertyMock(return_value=self.api_url)
        type(self.mock_auth).api_url = url_p
        self.c = MaasClient(self.mock_auth)

    def test_get_no_tags(self, mock_requests, mock_oauth):
        setup_mock_response(mock_requests, ok=True, response="[]")
        self.assertEqual(self.c.tags, [])

    def test_get_tags(self, mock_requests, mock_oauth):
        setup_mock_response(mock_requests, ok=True,
                            response=TWO_TAGS_JSON)
        self.assertEqual(self.c.tags[0]['name'], 'tag1')
        self.assertEqual(self.c.tags[1]['name'], 'tag2')

    def test_create_tag_existing(self, mock_requests, mock_oauth):
        setup_mock_response(mock_requests, op='get', ok=True,
                            response=TWO_TAGS_JSON)
        setup_mock_response(mock_requests, op='post', ok=True)
        rv = self.c.tag_new('tag1')
        self.assertEqual(rv, False)
        self.assertEqual(mock_requests.post.call_count, 0)

    def test_create_tag_nonexisting(self, mock_requests, mock_oauth):
        setup_mock_response(mock_requests, op='get', ok=True,
                            response=TWO_TAGS_JSON)
        setup_mock_response(mock_requests, op='post', ok=True)

        mock_oauth.return_value = sentinel.OAUTH

        newtag = 'newtag'
        rv = self.c.tag_new(newtag)
        self.assertEqual(True, rv)
        mock_requests.post.assert_called_once_with(url=self.api_url + '/tags/',
                                                   auth=sentinel.OAUTH,
                                                   data=dict(op='new',
                                                             name=newtag))

    def test_tag_name(self, mock_requests, mock_oauth):

        system_id = "node-01"

        with patch('maasclient.MaasClient.tag_new') as mock_tag_new:
            with patch('maasclient.MaasClient.tag_machine') as mock_tag_mach:
                c = MaasClient(self.mock_auth)
                c.tag_name(json.loads(ONE_MACHINE_JSON_NOTAGS))

                mock_tag_new.assert_called_once_with(system_id)
                mock_tag_mach.assert_called_once_with(system_id,
                                                      system_id)

    def test_tag_fpi(self, mock_requests, mock_oauth):

        system_id = "node-01"
        fpi_tag = 'use-fastpath-installer'

        with patch('maasclient.MaasClient.tag_new') as mock_tag_new:
            with patch('maasclient.MaasClient.tag_machine') as mock_tag_mach:
                c = MaasClient(self.mock_auth)
                c.tag_fpi(json.loads(ONE_MACHINE_JSON_NOTAGS))

                mock_tag_new.assert_called_once_with(fpi_tag)
                mock_tag_mach.assert_called_once_with(fpi_tag,
                                                      system_id)
class MaasClientTagTest(unittest.TestCase):
    def setUp(self):
        self.mock_auth = MagicMock()
        self.api_url = "apiurl"
        url_p = PropertyMock(return_value=self.api_url)
        type(self.mock_auth).api_url = url_p
        self.c = MaasClient(self.mock_auth)

    def test_get_no_tags(self, mock_requests, mock_oauth):
        setup_mock_response(mock_requests, ok=True, response="[]")
        self.assertEqual(self.c.tags, [])

    def test_get_tags(self, mock_requests, mock_oauth):
        setup_mock_response(mock_requests, ok=True, response=TWO_TAGS_JSON)
        self.assertEqual(self.c.tags[0]["name"], "tag1")
        self.assertEqual(self.c.tags[1]["name"], "tag2")

    def test_create_tag_existing(self, mock_requests, mock_oauth):
        setup_mock_response(mock_requests, op="get", ok=True, response=TWO_TAGS_JSON)
        setup_mock_response(mock_requests, op="post", ok=True)
        rv = self.c.tag_new("tag1")
        self.assertEqual(rv, False)
        self.assertEqual(mock_requests.post.call_count, 0)

    def test_create_tag_nonexisting(self, mock_requests, mock_oauth):
        setup_mock_response(mock_requests, op="get", ok=True, response=TWO_TAGS_JSON)
        setup_mock_response(mock_requests, op="post", ok=True)

        mock_oauth.return_value = sentinel.OAUTH

        newtag = "newtag"
        rv = self.c.tag_new(newtag)
        self.assertEqual(True, rv)
        mock_requests.post.assert_called_once_with(
            url=self.api_url + "/tags/", auth=sentinel.OAUTH, data=dict(op="new", name=newtag)
        )

    def test_tag_name(self, mock_requests, mock_oauth):

        system_id = "node-01"

        with patch("maasclient.MaasClient.tag_new") as mock_tag_new:
            with patch("maasclient.MaasClient.tag_machine") as mock_tag_mach:
                c = MaasClient(self.mock_auth)
                c.tag_name(json.loads(ONE_MACHINE_JSON_NOTAGS))

                mock_tag_new.assert_called_once_with(system_id)
                mock_tag_mach.assert_called_once_with(system_id, system_id)

    def test_tag_fpi(self, mock_requests, mock_oauth):

        system_id = "node-01"
        fpi_tag = "use-fastpath-installer"

        with patch("maasclient.MaasClient.tag_new") as mock_tag_new:
            with patch("maasclient.MaasClient.tag_machine") as mock_tag_mach:
                c = MaasClient(self.mock_auth)
                c.tag_fpi(json.loads(ONE_MACHINE_JSON_NOTAGS))

                mock_tag_new.assert_called_once_with(fpi_tag)
                mock_tag_mach.assert_called_once_with(fpi_tag, system_id)