##// END OF EJS Templates
integrations: fix bug where deleting a repo did not delete integrations
dan -
r427:b1411a81 default
parent child Browse files
Show More
@@ -0,0 +1,78 b''
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2010-2016 RhodeCode GmbH
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
21 import pytest
22 import requests
23 from mock import Mock, patch
24
25 from rhodecode import events
26 from rhodecode.model.db import Session, Integration
27 from rhodecode.model.integration import IntegrationModel
28 from rhodecode.integrations.types.base import IntegrationTypeBase
29
30
31 class TestIntegrationType(IntegrationTypeBase):
32 """ Test integration type class """
33
34 key = 'test-integration'
35 display_name = 'Test integration type'
36
37 def __init__(self, settings):
38 super(IntegrationTypeBase, self).__init__(settings)
39 self.sent_events = [] # for testing
40
41 def send_event(self, event):
42 self.sent_events.append(event)
43
44
45 @pytest.fixture
46 def repo_integration_stub(request, repo_stub):
47 settings = {'test_key': 'test_value'}
48 integration = IntegrationModel().create(
49 TestIntegrationType, settings=settings, repo=repo_stub, enabled=True,
50 name='test repo integration')
51
52 @request.addfinalizer
53 def cleanup():
54 IntegrationModel().delete(integration)
55
56 return integration
57
58
59 @pytest.fixture
60 def global_integration_stub(request):
61 settings = {'test_key': 'test_value'}
62 integration = IntegrationModel().create(
63 TestIntegrationType, settings=settings, enabled=True,
64 name='test global integration')
65
66 @request.addfinalizer
67 def cleanup():
68 IntegrationModel().delete(integration)
69
70 return integration
71
72
73 def test_delete_repo_with_integration_deletes_integration(repo_integration):
74 Session().delete(repo_integration.repo)
75 Session().commit()
76 Session().expire_all()
77 assert Integration.get(repo_integration.integration_id) is None
78
@@ -1330,6 +1330,8 b' class Repository(Base, BaseModel):'
1330 cascade="all, delete, delete-orphan")
1330 cascade="all, delete, delete-orphan")
1331 ui = relationship('RepoRhodeCodeUi', cascade="all")
1331 ui = relationship('RepoRhodeCodeUi', cascade="all")
1332 settings = relationship('RepoRhodeCodeSetting', cascade="all")
1332 settings = relationship('RepoRhodeCodeSetting', cascade="all")
1333 integrations = relationship('Integration',
1334 cascade="all, delete, delete-orphan")
1333
1335
1334 def __unicode__(self):
1336 def __unicode__(self):
1335 return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id,
1337 return u"<%s('%s:%s')>" % (self.__class__.__name__, self.repo_id,
@@ -3495,6 +3497,11 b' class Integration(Base, BaseModel):'
3495 nullable=True, unique=None, default=None)
3497 nullable=True, unique=None, default=None)
3496 repo = relationship('Repository', lazy='joined')
3498 repo = relationship('Repository', lazy='joined')
3497
3499
3500 def __init__(self, **kw):
3501 settings = kw.pop('settings', {})
3502 self.settings = settings
3503 super(Integration, self).__init__(**kw)
3504
3498 @hybrid_property
3505 @hybrid_property
3499 def settings(self):
3506 def settings(self):
3500 data = json.loads(self.settings_json or '{}')
3507 data = json.loads(self.settings_json or '{}')
@@ -42,6 +42,7 b' from rhodecode.model import BaseModel'
42 from rhodecode.model.db import Integration, User
42 from rhodecode.model.db import Integration, User
43 from rhodecode.model.meta import Session
43 from rhodecode.model.meta import Session
44 from rhodecode.integrations import integration_type_registry
44 from rhodecode.integrations import integration_type_registry
45 from rhodecode.integrations.types.base import IntegrationTypeBase
45
46
46 log = logging.getLogger(__name__)
47 log = logging.getLogger(__name__)
47
48
@@ -60,11 +61,24 b' class IntegrationModel(BaseModel):'
60 raise Exception('integration must be int, long or Instance'
61 raise Exception('integration must be int, long or Instance'
61 ' of Integration got %s' % type(integration))
62 ' of Integration got %s' % type(integration))
62
63
64 def create(self, IntegrationType, enabled, name, settings, repo=None):
65 """ Create an IntegrationType integration """
66 integration = Integration(
67 integration_type=IntegrationType.key,
68 settings={},
69 repo=repo,
70 enabled=enabled,
71 name=name
72 )
73 self.sa.add(integration)
74 self.sa.commit()
75 return integration
76
63 def delete(self, integration):
77 def delete(self, integration):
64 try:
78 try:
65 integration = self.__get_integration(integration)
79 integration = self.__get_integration(integration)
66 if integration:
80 if integration:
67 Session().delete(integration)
81 self.sa.delete(integration)
68 return True
82 return True
69 except Exception:
83 except Exception:
70 log.error(traceback.format_exc())
84 log.error(traceback.format_exc())
General Comments 0
You need to be logged in to leave comments. Login now