def test_gitlab_api_create_release_webhook(m_gitlab, app):
    app.config['DEBUG'] = False

    class MockHooksManager:
        def create(self, data):
            assert data == {
                'url': 'http://analysispreservation.cern.ch/api/repos/event',
                'token': 'mysecret',
                'push_events': False,
                'tag_push_events': True
            }
            return Mock(id=12345)

    class MockBranchManager:
        def get(self, name):
            m = Mock(commit=dict(id='mybranchsha'))
            m.name = 'mybranch'
            return m

    class MockProjectManager:
        def get(self, name, lazy):
            return Mock(branches=MockBranchManager(),
                        hooks=MockHooksManager(),
                        id='123')

    m_gitlab.return_value = Mock(projects=MockProjectManager())

    api = GitlabAPI('gitlab.cern.ch', 'owner_name', 'myrepository', 'mybranch')

    assert api.create_webhook() == (12345, 'mysecret')
def test_gitlab_api_create_webhook_when_no_permissions_to_create_a_webhook_raises_GitIntegrationError(
        m_gitlab, app):
    class MockHooksManager:
        def create(self, data):
            raise GitlabAuthenticationError('Unauthorized', 401)

    class MockBranchManager:
        def get(self, name):
            m = Mock(commit=dict(id='mybranchsha'))
            m.name = 'mybranch'
            return m

    class MockProjectManager:
        def get(self, name, lazy):
            return Mock(branches=MockBranchManager(),
                        hooks=MockHooksManager(),
                        id='123')

    m_gitlab.return_value = Mock(projects=MockProjectManager())

    api = GitlabAPI('gitlab.cern.ch', 'owner_name', 'myrepository', 'mybranch')

    with raises(GitIntegrationError):
        api.create_webhook() == (12345, 'mysecret')