##// END OF EJS Templates
events: fix bugs with serialization of repo/pr events and add tests for those cases
dan -
r389:06163eeb default
parent child
Show More
@@ -212,6 +212,7 def includeme(config):
212 config.include('pyramid_mako')
212 config.include('pyramid_mako')
213 config.include('pyramid_beaker')
213 config.include('pyramid_beaker')
214 config.include('rhodecode.admin')
214 config.include('rhodecode.admin')
215 config.include('rhodecode.integrations')
215 config.include('rhodecode.authentication')
216 config.include('rhodecode.authentication')
216 config.include('rhodecode.login')
217 config.include('rhodecode.login')
217 config.include('rhodecode.tweens')
218 config.include('rhodecode.tweens')
@@ -39,8 +39,9 class RhodecodeEventSchema(Schema):
39 Marshmallow schema for a rhodecode event
39 Marshmallow schema for a rhodecode event
40 """
40 """
41 utc_timestamp = fields.DateTime()
41 utc_timestamp = fields.DateTime()
42 acting_user = fields.Nested(UserSchema)
42 actor = fields.Nested(UserSchema)
43 acting_ip = fields.Str()
43 actor_ip = fields.Str()
44 name = fields.Str(attribute='name')
44
45
45
46
46 class RhodecodeEvent(object):
47 class RhodecodeEvent(object):
@@ -54,13 +55,13 class RhodecodeEvent(object):
54 self.utc_timestamp = datetime.utcnow()
55 self.utc_timestamp = datetime.utcnow()
55
56
56 @property
57 @property
57 def acting_user(self):
58 def actor(self):
58 if self.request:
59 if self.request:
59 return self.request.user.get_instance()
60 return self.request.user.get_instance()
60 return SYSTEM_USER
61 return SYSTEM_USER
61
62
62 @property
63 @property
63 def acting_ip(self):
64 def actor_ip(self):
64 if self.request:
65 if self.request:
65 return self.request.user.ip_addr
66 return self.request.user.ip_addr
66 return '<no ip available>'
67 return '<no ip available>'
@@ -18,11 +18,11
18
18
19 from marshmallow import Schema, fields
19 from marshmallow import Schema, fields
20
20
21 from rhodecode.model.db import Repository, Session
21 from rhodecode.model.db import User, Repository, Session
22 from rhodecode.events.base import RhodecodeEvent
22 from rhodecode.events.base import RhodecodeEvent
23
23
24
24
25 def get_pull_request_url(repo):
25 def get_repo_url(repo):
26 from rhodecode.model.repo import RepoModel
26 from rhodecode.model.repo import RepoModel
27 return RepoModel().get_url(repo)
27 return RepoModel().get_url(repo)
28
28
@@ -33,14 +33,14 class RepositorySchema(Schema):
33 """
33 """
34 repo_id = fields.Integer()
34 repo_id = fields.Integer()
35 repo_name = fields.Str()
35 repo_name = fields.Str()
36 url = fields.Function(get_pull_request_url)
36 url = fields.Function(get_repo_url)
37
37
38
38
39 class RepoEventSchema(RhodecodeEvent.MarshmallowSchema):
39 class RepoEventSchema(RhodecodeEvent.MarshmallowSchema):
40 """
40 """
41 Marshmallow schema for a repository event
41 Marshmallow schema for a repository event
42 """
42 """
43 repository = fields.Nested(RepositorySchema)
43 repo = fields.Nested(RepositorySchema)
44
44
45
45
46 class RepoEvent(RhodecodeEvent):
46 class RepoEvent(RhodecodeEvent):
@@ -100,14 +100,14 class RepoVCSEvent(RepoEvent):
100 super(RepoVCSEvent, self).__init__(self.repo)
100 super(RepoVCSEvent, self).__init__(self.repo)
101
101
102 @property
102 @property
103 def acting_user(self):
103 def actor(self):
104 if self.extras.get('username'):
104 if self.extras.get('username'):
105 return User.get_by_username(extras['username'])
105 return User.get_by_username(self.extras['username'])
106
106
107 @property
107 @property
108 def acting_ip(self):
108 def actor_ip(self):
109 if self.extras.get('ip'):
109 if self.extras.get('ip'):
110 return User.get_by_username(extras['ip'])
110 return self.extras['ip']
111
111
112
112
113 class RepoPrePullEvent(RepoVCSEvent):
113 class RepoPrePullEvent(RepoVCSEvent):
@@ -749,7 +749,7 class PullRequestModel(BaseModel):
749 return ids_to_add, ids_to_remove
749 return ids_to_add, ids_to_remove
750
750
751 def get_url(self, pull_request):
751 def get_url(self, pull_request):
752 return url('pullrequest_show', repo_name=self.target_repo.repo_name,
752 return h.url('pullrequest_show', repo_name=self.target_repo.repo_name,
753 pull_request_id=self.pull_request_id,
753 pull_request_id=self.pull_request_id,
754 qualified=True)
754 qualified=True)
755
755
@@ -142,7 +142,7 class RepoModel(BaseModel):
142 return None
142 return None
143
143
144 def get_url(self, repo):
144 def get_url(self, repo):
145 return url('summary_home', repo_name=repo.repo_name, qualified=True)
145 return h.url('summary_home', repo_name=repo.repo_name, qualified=True)
146
146
147 def get_users(self, name_contains=None, limit=20, only_active=True):
147 def get_users(self, name_contains=None, limit=20, only_active=True):
148 # TODO: mikhail: move this method to the UserModel.
148 # TODO: mikhail: move this method to the UserModel.
@@ -31,6 +31,23 from rhodecode.events import (
31 PullRequestCloseEvent,
31 PullRequestCloseEvent,
32 )
32 )
33
33
34 # TODO: dan: make the serialization tests complete json comparisons
35 @pytest.mark.backends("git", "hg")
36 @pytest.mark.parametrize('EventClass', [
37 PullRequestCreateEvent,
38 PullRequestUpdateEvent,
39 PullRequestReviewEvent,
40 PullRequestMergeEvent,
41 PullRequestCloseEvent,
42 ])
43 def test_pullrequest_events_serialized(pr_util, EventClass):
44 pr = pr_util.create_pull_request()
45 event = EventClass(pr)
46 data = event.as_dict()
47 assert data['name'] == EventClass.name
48 assert data['repo']['repo_name'] == pr.target_repo.repo_name
49 assert data['pullrequest']['pull_request_id'] == pr.pull_request_id
50
34
51
35 @pytest.mark.backends("git", "hg")
52 @pytest.mark.backends("git", "hg")
36 def test_create_pull_request_events(pr_util):
53 def test_create_pull_request_events(pr_util):
@@ -49,6 +49,39 def scm_extras(user_regular, repo_stub):
49 return extras
49 return extras
50
50
51
51
52 # TODO: dan: make the serialization tests complete json comparisons
53 @pytest.mark.parametrize('EventClass', [
54 RepoPreCreateEvent, RepoCreatedEvent,
55 RepoPreDeleteEvent, RepoDeletedEvent,
56 ])
57 def test_repo_events_serialized(repo_stub, EventClass):
58 event = EventClass(repo_stub)
59 data = event.as_dict()
60 assert data['name'] == EventClass.name
61 assert data['repo']['repo_name'] == repo_stub.repo_name
62
63
64 @pytest.mark.parametrize('EventClass', [
65 RepoPrePullEvent, RepoPullEvent, RepoPrePushEvent
66 ])
67 def test_vcs_repo_events_serialize(repo_stub, scm_extras, EventClass):
68 event = EventClass(repo_name=repo_stub.repo_name, extras=scm_extras)
69 data = event.as_dict()
70 assert data['name'] == EventClass.name
71 assert data['repo']['repo_name'] == repo_stub.repo_name
72
73
74
75 @pytest.mark.parametrize('EventClass', [RepoPushEvent])
76 def test_vcs_repo_events_serialize(repo_stub, scm_extras, EventClass):
77 event = EventClass(repo_name=repo_stub.repo_name,
78 pushed_commit_ids=scm_extras['commit_ids'],
79 extras=scm_extras)
80 data = event.as_dict()
81 assert data['name'] == EventClass.name
82 assert data['repo']['repo_name'] == repo_stub.repo_name
83
84
52 def test_create_delete_repo_fires_events(backend):
85 def test_create_delete_repo_fires_events(backend):
53 with EventCatcher() as event_catcher:
86 with EventCatcher() as event_catcher:
54 repo = backend.create_repo()
87 repo = backend.create_repo()
@@ -77,3 +110,4 def test_push_fires_events(scm_extras):
77 with EventCatcher() as event_catcher:
110 with EventCatcher() as event_catcher:
78 hooks_base.post_pull(scm_extras)
111 hooks_base.post_pull(scm_extras)
79 assert event_catcher.events_types == [RepoPullEvent]
112 assert event_catcher.events_types == [RepoPullEvent]
113
General Comments 0
You need to be logged in to leave comments. Login now