# Copyright 2012 OpenStack Foundation
#
# 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.
"""
To run these tests against a live database:

1. Modify the file `keystone/tests/unit/config_files/backend_sql.conf` to use
   the connection for your live database.
2. Set up a blank, live database.
3. Run the tests using::

    tox -e py27 -- keystone.tests.unit.test_sql_migrate_extensions

WARNING::

   Your database will be wiped.

   Do not do this against a Database with valuable data as
   all data will be lost.
"""

from keystone.contrib import endpoint_filter
from keystone.contrib import endpoint_policy
from keystone.contrib import example
from keystone.contrib import federation
from keystone.contrib import oauth1
from keystone.contrib import revoke
from keystone import exception
from keystone.tests.unit import test_sql_upgrade


class SqlUpgradeExampleExtension(test_sql_upgrade.SqlMigrateBase):
    def repo_package(self):
        return example

    def test_upgrade(self):
        self.assertTableDoesNotExist('example')
        self.upgrade(1, repository=self.repo_path)
        self.assertTableColumns('example', ['id', 'type', 'extra'])


class SqlUpgradeOAuth1Extension(test_sql_upgrade.SqlMigrateBase):
    def repo_package(self):
        return oauth1

    def test_upgrade(self):
        # We have 5 oauth1 migrations, each should fail
        for version in range(5):
            v = version + 1
            self.assertRaises(exception.MigrationMovedFailure,
                              self.upgrade, version=v,
                              repository=self.repo_path)


class EndpointFilterExtension(test_sql_upgrade.SqlMigrateBase):
    def repo_package(self):
        return endpoint_filter

    def test_upgrade(self):
        for version in range(2):
            v = version + 1
            self.assertRaises(exception.MigrationMovedFailure,
                              self.upgrade, version=v,
                              repository=self.repo_path)


class EndpointPolicyExtension(test_sql_upgrade.SqlMigrateBase):
    def repo_package(self):
        return endpoint_policy

    def test_upgrade(self):
        self.assertRaises(exception.MigrationMovedFailure,
                          self.upgrade, version=1,
                          repository=self.repo_path)


class FederationExtension(test_sql_upgrade.SqlMigrateBase):

    FEDERATION_MIGRATIONS = 8

    def repo_package(self):
        return federation

    def test_upgrade(self):
        for version in range(self.FEDERATION_MIGRATIONS):
            v = version + 1
            self.assertRaises(exception.MigrationMovedFailure,
                              self.upgrade, version=v,
                              repository=self.repo_path)


class RevokeExtension(test_sql_upgrade.SqlMigrateBase):

    def repo_package(self):
        return revoke

    def test_upgrade(self):
        for version in range(2):
            v = version + 1
            self.assertRaises(exception.MigrationMovedFailure,
                              self.upgrade, version=v,
                              repository=self.repo_path)

@@ -1704,32 +1704,60 @@
 qlMigrateBase):%0A
+%0A    OAUTH1_MIGRATIONS = 5%0A%0A
     def repo_pac
@@ -1823,64 +1823,8 @@
 f):%0A
-        # We have 5 oauth1 migrations, each should fail%0A
     
@@ -1848,17 +1848,38 @@
 n range(
-5
+self.OAUTH1_MIGRATIONS
 ):%0A     
@@ -2134,32 +2134,69 @@
 qlMigrateBase):%0A
+%0A    ENDPOINT_FILTER_MIGRATIONS = 2%0A%0A
     def repo_pac
@@ -2288,33 +2288,63 @@
 ersion in range(
-2
+self.ENDPOINT_FILTER_MIGRATIONS
 ):%0A            v
@@ -2591,32 +2591,69 @@
 qlMigrateBase):%0A
+%0A    ENDPOINT_POLICY_MIGRATIONS = 1%0A%0A
     def repo_pac
@@ -2826,18 +2826,74 @@
 ade,
- version=1
+%0A                          version=self.ENDPOINT_POLICY_MIGRATIONS
 ,%0A  
@@ -3436,24 +3436,51 @@
 rateBase):%0A%0A
+    REVOKE_MIGRATIONS = 2%0A%0A
     def repo
@@ -3575,17 +3575,38 @@
 n range(
-2
+self.REVOKE_MIGRATIONS
 ):%0A     

remove use of magic numbers in sql migrate extension tests