#
# Copyright (c) SAS Institute Inc.
#
# 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 rbuild import errors
from collections import namedtuple
from rbuild_test import rbuildhelp
from testutils import mock


class CreateProjectBranchTest(rbuildhelp.RbuildHelper):

    def testCreateProjectArgParse(self):
        self.getRbuildHandle()
        self.checkRbuild('create project --name=title --short-name=short '
                '--domain-name=project.domain --description=description',
            'rbuild_plugins.createprojectbranch.CreateProjectCommand.runCommand',
            [None, None, {
                'name': 'title',
                'short-name': 'short',
                'domain-name': 'project.domain',
                'description': 'description',
            }, ['create', 'project']])

        self.checkRbuild('create project --name=title --short-name=short '
                '--domain-name=project.domain --description=description '
                '--external --upstream-url other.domain --auth-type auth '
                '--username user --password secret --entitlement entitle',
            'rbuild_plugins.createprojectbranch.CreateProjectCommand.runCommand',
            [None, None, {
                'name': 'title',
                'short-name': 'short',
                'domain-name': 'project.domain',
                'description': 'description',
                'external': True,
                'upstream-url': 'other.domain',
                'auth-type': 'auth',
                'username': 'user',
                'password': 'secret',
                'entitlement': 'entitle',
            }, ['create', 'project']])

    def testCreateProjectCmdline(self):
        handle = self.getRbuildHandle(mock.MockObject())
        handle.Create.registerCommands()
        handle.CreateProjectBranch.initialize()
        mock.mockMethod(handle.facade.rbuilder.createProject)
        cmd = handle.Commands.getCommandClass('create')()
        cmd.runCommand(handle, {
            'name': 'project name',
            'short-name': 'shortname',
            'domain-name': '',
            }, ['rbuild', 'create', 'project'])
        handle.facade.rbuilder.createProject._mock.assertCalled(
                title='project name',
                shortName='shortname',
                domainName='',
                description='',
                )

    def testCreateExternalProjectCmdline(self):
        handle = self.getRbuildHandle(mock.MockObject())
        handle.Create.registerCommands()
        handle.CreateProjectBranch.initialize()
        mock.mockMethod(handle.facade.rbuilder.createProject)
        cmd = handle.Commands.getCommandClass('create')()

        # no auth
        cmd.runCommand(handle, {
            'name': 'project name',
            'short-name': 'shortname',
            'domain-name': '',
            'external': True,
            'label': 'repo@n:branch',
            'upstream-url': 'http://foo.com',
            'auth-type': 'none',
            }, ['rbuild', 'create', 'project'])
        handle.facade.rbuilder.createProject._mock.assertCalled(
                title='project name',
                shortName='shortname',
                domainName='',
                description='',