Add more Zone Transfer tests

Port tests from Designate repo

Change-Id: I843701655f3fd07245b79e37fa286f05f20bf7a3
Depends-On: I5fdefa64480f118dad898ed4651036f9b9b16fe9
Depends-On: Id2d093891953efcbb125560ea1113b8a9e613a9c
diff --git a/designate_tempest_plugin/services/dns/v2/json/transfer_request_client.py b/designate_tempest_plugin/services/dns/v2/json/transfer_request_client.py
index 513a8ad..f494cde 100644
--- a/designate_tempest_plugin/services/dns/v2/json/transfer_request_client.py
+++ b/designate_tempest_plugin/services/dns/v2/json/transfer_request_client.py
@@ -42,6 +42,24 @@
         return resp, body
 
     @base.handle_errors
+    def create_transfer_request_empty_body(self, uuid, params=None):
+        """Create a zone transfer_requests.
+        :param uuid: Unique identifier of the zone in UUID format.
+        :param params: A Python dict that represents the query paramaters to
+                       include in the request URI.
+        :return: Serialized zone trasfer request as a dictionary.
+        """
+
+        transfer_request_uri = 'zones/{0}/tasks/transfer_requests'.format(uuid)
+        resp, body = self._create_request(
+            transfer_request_uri, None, params=params)
+
+        # Create Transfer request should Return a HTTP 201
+        self.expected_success(201, resp.status)
+
+        return resp, body
+
+    @base.handle_errors
     def show_transfer_request(self, uuid, params=None):
         """Gets a specific transfer_requestsed zone.
         :param uuid: Unique identifier of the transfer_requestsed zone in
diff --git a/designate_tempest_plugin/tests/api/v2/test_transfer_accepts.py b/designate_tempest_plugin/tests/api/v2/test_transfer_accepts.py
index a043137..0972a24 100644
--- a/designate_tempest_plugin/tests/api/v2/test_transfer_accepts.py
+++ b/designate_tempest_plugin/tests/api/v2/test_transfer_accepts.py
@@ -68,8 +68,8 @@
                         transfer_request['id'])
 
         data = {
-                 "key": transfer_request['key'],
-                 "zone_transfer_request_id": transfer_request['id']
+            "key": transfer_request['key'],
+            "zone_transfer_request_id": transfer_request['id']
         }
 
         LOG.info('Create a zone transfer_accept')
diff --git a/designate_tempest_plugin/tests/api/v2/test_transfer_request.py b/designate_tempest_plugin/tests/api/v2/test_transfer_request.py
index 9bc3897..af81cfc 100644
--- a/designate_tempest_plugin/tests/api/v2/test_transfer_request.py
+++ b/designate_tempest_plugin/tests/api/v2/test_transfer_request.py
@@ -17,22 +17,25 @@
 from tempest.lib import exceptions as lib_exc
 
 from designate_tempest_plugin.tests import base
+from designate_tempest_plugin import data_utils as dns_data_utils
 
 LOG = logging.getLogger(__name__)
 
 
 class BaseTransferRequestTest(base.BaseDnsV2Test):
-    excluded_keys = ['created_at', 'updated_at', 'key', 'links',
-                    'zone_name']
+    excluded_keys = ['created_at', 'updated_at', 'key', 'links']
 
 
 class TransferRequestTest(BaseTransferRequestTest):
+    credentials = ['primary', 'alt']
+
     @classmethod
     def setup_clients(cls):
         super(TransferRequestTest, cls).setup_clients()
 
         cls.zone_client = cls.os.zones_client
         cls.client = cls.os.transfer_request_client
+        cls.alt_client = cls.os_alt.transfer_request_client
 
     @decorators.idempotent_id('2381d489-ad84-403d-b0a2-8b77e4e966bf')
     def test_create_transfer_request(self):
@@ -48,6 +51,38 @@
         LOG.info('Ensure we respond with ACTIVE status')
         self.assertEqual('ACTIVE', transfer_request['status'])
 
+    @decorators.idempotent_id('5deae1ac-7c14-42dc-b14e-4e4b2725beb7')
+    def test_create_transfer_request_scoped(self):
+        LOG.info('Create a zone')
+        _, zone = self.zone_client.create_zone()
+        self.addCleanup(self.zone_client.delete_zone, zone['id'])
+
+        transfer_request_data = dns_data_utils.rand_transfer_request_data(
+            target_project_id=self.os_alt.credentials.project_id)
+
+        LOG.info('Create a scoped zone transfer_request')
+        _, transfer_request = self.client.create_transfer_request(
+            zone['id'], transfer_request_data)
+        self.addCleanup(self.client.delete_transfer_request,
+                        transfer_request['id'])
+
+        LOG.info('Ensure we respond with ACTIVE status')
+        self.assertEqual('ACTIVE', transfer_request['status'])
+
+    @decorators.idempotent_id('4505152f-0a9c-4f02-b385-2216c914a0be')
+    def test_create_transfer_request_empty_body(self):
+        LOG.info('Create a zone')
+        _, zone = self.zone_client.create_zone()
+        self.addCleanup(self.zone_client.delete_zone, zone['id'])
+        LOG.info('Create a zone transfer_request')
+        _, transfer_request = self.client.create_transfer_request_empty_body(
+            zone['id'])
+        self.addCleanup(self.client.delete_transfer_request,
+                        transfer_request['id'])
+
+        LOG.info('Ensure we respond with ACTIVE status')
+        self.assertEqual('ACTIVE', transfer_request['status'])
+
     @decorators.idempotent_id('64a7be9f-8371-4ce1-a242-c1190de7c985')
     def test_show_transfer_request(self):
         LOG.info('Create a zone')
@@ -66,6 +101,32 @@
                  'created transfer_request')
         self.assertExpected(transfer_request, body, self.excluded_keys)
 
+    @decorators.idempotent_id('235ded87-0c47-430b-8cad-4f3194b927a6')
+    def test_show_transfer_request_as_target(self):
+        # Checks the target of a scoped transfer request can see
+        # the request.
+        LOG.info('Create a zone')
+        _, zone = self.zone_client.create_zone()
+        self.addCleanup(self.zone_client.delete_zone, zone['id'])
+
+        transfer_request_data = dns_data_utils.rand_transfer_request_data(
+            target_project_id=self.os_alt.credentials.project_id)
+
+        LOG.info('Create a scoped zone transfer_request')
+        _, transfer_request = self.client.create_transfer_request(
+            zone['id'], transfer_request_data)
+        self.addCleanup(self.client.delete_transfer_request,
+                        transfer_request['id'])
+
+        LOG.info('Fetch the transfer_request as the target')
+        _, body = self.alt_client.show_transfer_request(transfer_request['id'])
+
+        LOG.info('Ensure the fetched response matches the '
+                 'created transfer_request')
+        excluded_keys = self.excluded_keys + ["target_project_id",
+                                              "project_id"]
+        self.assertExpected(transfer_request, body, excluded_keys)
+
     @decorators.idempotent_id('7d81c487-aa15-44c4-b3e5-424ab9e6a3e5')
     def test_delete_transfer_request(self):
         LOG.info('Create a zone')
diff --git a/designate_tempest_plugin/tests/scenario/v2/test_zones_transfer.py b/designate_tempest_plugin/tests/scenario/v2/test_zones_transfer.py
new file mode 100644
index 0000000..4870307
--- /dev/null
+++ b/designate_tempest_plugin/tests/scenario/v2/test_zones_transfer.py
@@ -0,0 +1,62 @@
+# Copyright 2016 Rackspace
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+from oslo_log import log as logging
+from tempest.lib import decorators
+from tempest.lib import exceptions as lib_exc
+
+from designate_tempest_plugin.tests import base
+
+LOG = logging.getLogger(__name__)
+
+
+class ZonesTransferTest(base.BaseDnsV2Test):
+    credentials = ['primary', 'alt']
+
+    @classmethod
+    def setup_clients(cls):
+        super(ZonesTransferTest, cls).setup_clients()
+        cls.zones_client = cls.os.zones_client
+        cls.alt_zones_client = cls.os_alt.zones_client
+        cls.request_client = cls.os.transfer_request_client
+        cls.alt_request_client = cls.os_alt.transfer_request_client
+        cls.accept_client = cls.os.transfer_accept_client
+        cls.alt_accept_client = cls.os_alt.transfer_accept_client
+
+    @decorators.idempotent_id('60bd80ac-c979-4686-9a03-f2f775f272ab')
+    def test_zone_transfer(self):
+        LOG.info('Create a zone as primary tenant')
+        _, zone = self.zones_client.create_zone()
+        self.addCleanup(self.zones_client.delete_zone, zone['id'],
+                        ignore_errors=lib_exc.NotFound)
+        self.addCleanup(self.alt_zones_client.delete_zone, zone['id'],
+                        ignore_errors=lib_exc.NotFound)
+
+        LOG.info('Create a zone transfer_request for zone as primary tenant')
+        _, transfer_request = \
+            self.request_client.create_transfer_request_empty_body(zone['id'])
+
+        accept_data = {
+                 "key": transfer_request['key'],
+                 "zone_transfer_request_id": transfer_request['id']
+        }
+
+        LOG.info('Accept the request as alt tenant')
+        self.alt_accept_client.create_transfer_accept(accept_data)
+
+        LOG.info('Fetch the zone as alt tenant')
+        self.alt_zones_client.show_zone(zone['id'])
+
+        LOG.info('Ensure 404 when fetching the zone as primary tenant')
+        self.assertRaises(lib_exc.NotFound,
+            lambda: self.zones_client.show_zone(zone['id']))