Skip to content

Commit 7dcabd4

Browse files
committed
Add test for person inbox and outbox
check activitypub content-type Fix create activity response test webfinger Fix review, note voting Signed-off-by: ziadhany <ziadhany2016@gmail.com>
1 parent fd06538 commit 7dcabd4

32 files changed

Lines changed: 1327 additions & 446 deletions

purl_sync/private.pem

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDHbwdd0Yj6pc6f
3+
IcMqXlQrtdi8CRQ2ixDqJK7wGS26Nj2Y/vVpXX68RTxZnM/UA/n3TYyinjQTsSkj
4+
557uGh4TcF+SCwVW77qdv4molAuKs7zwzkSetQl4XgxoSR5LuvJSusBYz1gtKr/z
5+
4tkME4YTo26P2kTjGXz9Jv58jtSBwIKkn12QJeCIEpmIpH++YUbfceO40bkrgCMP
6+
OoOyxuf0Q+aQoWfyuZC36EFFfegy00T0OSE3Z31v0NE7oMblM3d+FUG0y6vYaksn
7+
5q+XK+MI7+mqTrCx1r3sav2FitjPjxSW0tsg71SEs/j1Kv02GZLEovZFNO9+8NEm
8+
JBFKgJMbAgMBAAECggEAUKCvIjtlvSvOiqBMNnNXZSqKGo8zGFAFwRuejBwp1Ag8
9+
w/+DuRre83+sQ/B1R1pqMxSroE0EcdekccshAlMVyLccND93Y6vczwzSOxDLT8KV
10+
xuclb+JyFzMyofC5qlVExbSRsfX1yKDpZNDX/h1gjcz7lHXidsvlr7UVmsbl3CR/
11+
r9cJfRSCic8iR8rjB7yagswqw45wrMOdTK1yBCnIYIel4aXQHA7iIq8OPOujjoEl
12+
FPnVCm/Irj13dcGlRUDNrz15+f/bxScLDlYrEQ9FnwQb6lAb7PRnAbht89V5Xy8j
13+
/QH0S5ZgyIrG4iG6EdVa5IuUxtkuS4wCMOO1njwZcQKBgQDHjfSFF/lmdct0Cmsk
14+
LqG+B2994u0/KfVNDWR4xEvykC/tSVBUe2ZHGRH71ooEgvMI21ECWJTvqNvPb+Dv
15+
UfzbqLnGaosfCPrCGliQL+RW0LU+Uvrc7HklFdd3bWVRgxIltJrnhYMzxGXndc69
16+
Ms5/azzB0b+6SY8NrEkNXIXEUQKBgQD/2FNro+FAhrLgiTQM0xTcvXObaMSG4i5z
17+
s/ZzusalRTyTC/dD3UnnZU63/Iz+dEnOU3CnhwLLjk6mN5YNuNOrlkDOuMhLQN6c
18+
3HrHvhlfBxENAqiw9oIf6XnNRZjxtH6kQzEkSysAmgJLcQaTMoqQATRf74OEEpJQ
19+
G49d01ghqwKBgF9W2/8GgDpKhwSOwbeMq+vNQ6AiXfTyh+bjQiBMsHhYGP3GLCHg
20+
vvGy8ZufWVtkcNbHaYG0bpu90h7eNdpBEoyjpPY+Cu+t65f23F8u8mnj1uPK413P
21+
gOWN9N5zHpf+BhqLmgKJEDEYYRNffoF4F55azrFPE8ndwQHoURKYJLSxAoGBAJj2
22+
rpGJrrAUmLgUcBFG1o9PmeIN1opKirlUnIH+5quDZL8KBXEGyhvV/yWTknzD7Ci2
23+
zIDtAKfRUoagb0+O9Mb/E4pFAkOz6iTg0WvA5YxiPjiIIXGEQLJxtggbl2DTqG/v
24+
WF85reEzMzIFxoeVTTDh931wRzJcpCvO/n17UZL3AoGAXpvbfA9a+SuYLQkJ+4aW
25+
uwTcmg3aAO1s3r9OzUJw1O8KgDoIDpKo/pk5I8dkToqlggCBjJJMalCCY6nve7Ox
26+
9vGRIpgKYD0VdV7F92SIGT0cHOo8mHw2lFt4wW/R8/HCZ2zlnWQG+UYaNkWOyNRf
27+
1tU9T4shRLVpT/WlnCqddqQ=
28+
-----END PRIVATE KEY-----

purl_sync/public.pem

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx28HXdGI+qXOnyHDKl5U
3+
K7XYvAkUNosQ6iSu8BktujY9mP71aV1+vEU8WZzP1AP5902Mop40E7EpI+ee7hoe
4+
E3BfkgsFVu+6nb+JqJQLirO88M5EnrUJeF4MaEkeS7ryUrrAWM9YLSq/8+LZDBOG
5+
E6Nuj9pE4xl8/Sb+fI7UgcCCpJ9dkCXgiBKZiKR/vmFG33HjuNG5K4AjDzqDssbn
6+
9EPmkKFn8rmQt+hBRX3oMtNE9DkhN2d9b9DRO6DG5TN3fhVBtMur2GpLJ+avlyvj
7+
CO/pqk6wsda97Gr9hYrYz48UltLbIO9UhLP49Sr9NhmSxKL2RTTvfvDRJiQRSoCT
8+
GwIDAQAB
9+
-----END PUBLIC KEY-----

purl_sync/purl_sync/urls.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,25 @@
1919
from review.views import FollowView
2020
from review.views import HomeView
2121
from review.views import NoteView
22-
from review.views import PersonInbox
23-
from review.views import PersonOutbox
2422
from review.views import PersonSignUp
2523
from review.views import PersonView
2624
from review.views import PurlFollowers
2725
from review.views import PurlInbox
26+
from review.views import PurlListView
2827
from review.views import PurlOutbox
28+
from review.views import PurlProfile
2929
from review.views import PurlView
3030
from review.views import RepositoryListView
3131
from review.views import ReviewListView
3232
from review.views import ReviewView
3333
from review.views import UserFollowing
34+
from review.views import UserInbox
3435
from review.views import UserLogin
36+
from review.views import UserOutbox
37+
from review.views import UserProfile
3538
from review.views import WebfingerView
36-
from review.views import comment_vote
3739
from review.views import fetch_repository_file
40+
from review.views import note_vote
3841
from review.views import redirect_repository
3942
from review.views import redirect_vulnerability
4043
from review.views import review_vote
@@ -51,6 +54,7 @@
5154
path("accounts/logout", LogoutView.as_view(next_page="login"), name="logout"),
5255
path("create-repo", CreatGitView.as_view(), name="repo-create"),
5356
path("repo-list", RepositoryListView.as_view(), name="repo-list"),
57+
path("purl-list", PurlListView.as_view(), name="purl-list"),
5458
path(
5559
"repository/<uuid:repository_id>/create-review/",
5660
CreateReview.as_view(),
@@ -59,15 +63,17 @@
5963
path("review-list", ReviewListView.as_view(), name="review-list"),
6064
path("reviews/<uuid:review_id>/", ReviewView.as_view(), name="review-page"),
6165
path("reviews/<uuid:review_id>/votes/", review_vote, name="review-votes"),
62-
path("notes/<uuid:note_id>/votes/", comment_vote, name="comment-votes"),
66+
path("notes/<uuid:note_id>/votes/", note_vote, name="comment-votes"),
6367
path("repository/<uuid:repository_id>/", redirect_repository, name="repository-page"),
6468
path("repository/<uuid:repository_id>/fetch", fetch_repository_file, name="repository-fetch"),
6569
path(
6670
"vulnerability/<uuid:vulnerability_id>/", redirect_vulnerability, name="vulnerability-page"
6771
),
6872
path("notes/<uuid:uuid>", NoteView.as_view(), name="note-page"),
69-
path("api/v0/users/@<str:username>/inbox", PersonInbox.as_view(), name="user-inbox"),
70-
path("api/v0/users/@<str:username>/outbox", PersonOutbox.as_view(), name="user-outbox"),
73+
path("api/v0/users/@<str:username>", UserProfile.as_view(), name="user-ap-profile"),
74+
path("api/v0/purls/@<path:purl_string>/", PurlProfile.as_view(), name="purl-ap-profile"),
75+
path("api/v0/users/@<str:username>/inbox", UserInbox.as_view(), name="user-inbox"),
76+
path("api/v0/users/@<str:username>/outbox", UserOutbox.as_view(), name="user-outbox"),
7177
path("api/v0/purls/@<path:purl_string>/inbox", PurlInbox.as_view(), name="purl-inbox"),
7278
path("api/v0/purls/@<path:purl_string>/outbox", PurlOutbox.as_view(), name="purl-outbox"),
7379
path("api/v0/users/@<str:username>/following/", UserFollowing.as_view(), name="user-following"),
@@ -82,6 +88,5 @@
8288
path("o/", include("oauth2_provider.urls", namespace="oauth2_provider")),
8389
]
8490

85-
8691
if settings.DEBUG:
8792
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

purl_sync/review/activitypub.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"Vulnerability": Vulnerability,
4646
}
4747

48-
VALID_HEADERS = [
48+
AP_VALID_HEADERS = [
4949
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
5050
"application/activity+json",
5151
]
@@ -57,7 +57,7 @@
5757
AP_TARGET = {"cc": "https://www.w3.org/ns/activitystreams#Public"}
5858

5959

60-
def check_ap_context(request):
60+
def check_and_r_ap_context(request):
6161
"""
6262
check activitypub context request and return request without @context
6363
"""
@@ -73,7 +73,7 @@ def add_ap_target(response):
7373
Add target activitypub response
7474
"""
7575
if response is not dict:
76-
raise Exception("Invalid response")
76+
raise KeyError("Invalid response")
7777

7878
if not response.get("cc"):
7979
response.append(**AP_TARGET)
@@ -83,15 +83,15 @@ def add_ap_target(response):
8383

8484
def has_valid_header(view):
8585
"""
86-
check if the request header in the VALID_HEADERS if yes return view else return HttpResponseForbidden
86+
check if the request header in the AP_VALID_HEADERS if yes return view else return HttpResponseForbidden
8787
"""
8888

8989
def wrapper(request, *args, **kwargs):
9090
content_type = request.headers.get("Content-Type")
91-
if content_type in VALID_HEADERS:
91+
if content_type in AP_VALID_HEADERS:
9292
return view(request, *args, **kwargs)
9393
else:
94-
return HttpResponseForbidden()
94+
return
9595

9696
return wrapper
9797

@@ -135,10 +135,10 @@ def get_by_type(self):
135135

136136
def get(self):
137137
obj_id, page_name = full_resolve(self.id)
138-
if page_name == "purl-profile":
139-
return Purl.objects.get(string=obj_id["slug"])
140-
elif page_name == "user-profile":
141-
user = User.objects.get(username=obj_id["slug"])
138+
if page_name == "purl-ap-profile":
139+
return Purl.objects.get(string=obj_id["purl_string"])
140+
elif page_name == "user-ap-profile":
141+
user = User.objects.get(username=obj_id["username"])
142142
if hasattr(user, "person"):
143143
return user.person
144144
elif hasattr(user, "service"):
@@ -166,8 +166,8 @@ class ApObject:
166166
published: str = None
167167

168168
URL_MAPPER = {
169-
"user-profile": "slug",
170-
"purl-profile": "slug",
169+
"user-ap-profile": "username",
170+
"purl-ap-profile": "purl_string",
171171
"review-page": "uuid",
172172
"repository-page": "uuid",
173173
"note-page": "uuid",
@@ -194,7 +194,7 @@ def save(self):
194194
return self.failed_ap_rs()
195195
obj_id, page_name = full_resolve(self.object.id)
196196
if type(actor) is Person:
197-
purl = Purl.objects.get(string=obj_id["slug"])
197+
purl = Purl.objects.get(string=obj_id["purl_string"])
198198
new_obj, created = Follow.objects.get_or_create(person_id=actor.id, purl=purl)
199199
else:
200200
return self.failed_ap_rs()
@@ -270,7 +270,15 @@ def save(self):
270270

271271
def succeeded_ap_rs(self, new_obj):
272272
"""Response for successfully deleting the object"""
273-
return JsonResponse({"Location": new_obj.to_ap}, status=201)
273+
Obj_Map = {
274+
"Note": "note-page",
275+
"Review": "review-page",
276+
"Repository": "repository-page",
277+
"Vulnerability": "vulnerability-page",
278+
}
279+
return JsonResponse(
280+
{"Location": full_reverse(Obj_Map[self.object.type], new_obj.id)}, status=201
281+
)
274282

275283
def failed_ap_rs(self):
276284
"""Response for failure deleting the object"""
@@ -362,7 +370,7 @@ def failed_ap_rs(self):
362370
def create_activity_obj(data):
363371
"""Convert json object to activity object"""
364372
payload = json.loads(data)
365-
payload_without_context = check_ap_context(payload)
373+
payload_without_context = check_and_r_ap_context(payload)
366374
return Activity(**payload_without_context)
367375

368376

@@ -378,7 +386,7 @@ def save(self):
378386
return self.failed_ap_rs()
379387
else:
380388
obj_id, page_name = full_resolve(self.object.id)
381-
purl = Purl.objects.get(string=obj_id["slug"])
389+
purl = Purl.objects.get(string=obj_id["purl_string"])
382390
follow_obj = Follow.objects.get(person_id=actor.id, purl=purl)
383391
follow_obj.delete()
384392
return self.succeeded_ap_rs()

0 commit comments

Comments
 (0)