Skip to content

Commit b4a22d2

Browse files
authored
Merge pull request #260 from Mosquito-Alert/api_photos_endpoint
Added API endpoint for photos
2 parents 0d4c3b9 + 7fa1845 commit b4a22d2

File tree

14 files changed

+283
-10
lines changed

14 files changed

+283
-10
lines changed

api/serializers.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,4 +553,22 @@ def create(self, validated_data):
553553
instance.save()
554554
return instance
555555
class Meta(UserSerializer.Meta):
556-
fields = UserSerializer.Meta.fields + ("password",)
556+
fields = UserSerializer.Meta.fields + ("password",)
557+
558+
class PhotoSerializer(serializers.ModelSerializer):
559+
560+
image_path = serializers.SerializerMethodField()
561+
562+
def get_image_path(self, obj) -> str:
563+
return obj.photo.path
564+
565+
class Meta:
566+
model = Photo
567+
fields = (
568+
"uuid",
569+
"image_url",
570+
"image_path"
571+
)
572+
extra_kwargs = {
573+
"image_url": {"source": "photo"},
574+
}
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
from rest_framework.authtoken.models import Token
55

6+
from django.core.files.uploadedfile import SimpleUploadedFile
67
from django.contrib.auth import get_user_model
78
from django.utils import timezone
89

910
from api.tests.utils import grant_permission_to_user
10-
from tigaserver_app.models import EuropeCountry, TigaUser, Report
11+
from tigaserver_app.models import EuropeCountry, TigaUser, Report, Photo
1112

1213
User = get_user_model()
1314

@@ -23,10 +24,17 @@ def app_user(user_password):
2324
user.save(0)
2425
return user
2526

27+
@pytest.fixture
28+
def dummy_image():
29+
# Prepare a fake image file
30+
image_content = b"fake image content" # Replace with actual binary data if needed
31+
test_image = SimpleUploadedFile("test_image.jpg", image_content, content_type="image/jpeg")
32+
33+
return test_image
2634

2735
@pytest.fixture
28-
def adult_report(app_user):
29-
return Report.objects.create(
36+
def adult_report(app_user, dummy_image):
37+
r = Report.objects.create(
3038
user=app_user,
3139
report_id=1234, # TODO: change
3240
phone_upload_time=timezone.now(),
@@ -38,6 +46,16 @@ def adult_report(app_user):
3846
current_location_lat=2,
3947
)
4048

49+
_ = Photo.objects.create(
50+
photo=dummy_image,
51+
report=r,
52+
)
53+
54+
return r
55+
56+
@pytest.fixture
57+
def report_photo(adult_report):
58+
return adult_report.photos.first()
4159

4260
@pytest.fixture
4361
def django_live_url(live_server):
@@ -80,7 +98,6 @@ def user():
8098
password=User.objects.make_random_password(),
8199
)
82100

83-
84101
@pytest.fixture
85102
def token_instance_user(user):
86103
token, _ = Token.objects.get_or_create(user=user)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pytest
2+
3+
from tigaserver_app.models import Photo
4+
5+
6+
# NOTE: needed for token with perms fixture
7+
@pytest.fixture
8+
def model_class():
9+
return Photo
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
3+
test_name: Create is disabled for all users
4+
5+
includes:
6+
- !include schema.yml
7+
8+
marks:
9+
- usefixtures:
10+
- api_live_url
11+
- token_user_can_add
12+
13+
stages:
14+
- id: signup
15+
type: ref
16+
- id: login
17+
type: ref
18+
- name: Create method not exist for mobile users
19+
request:
20+
url: "{api_live_url}/{endpoint}/"
21+
headers:
22+
Authorization: "Bearer {token}"
23+
method: "POST"
24+
response:
25+
status_code: 404
26+
- name: Create method non authenticated users
27+
request:
28+
url: "{api_live_url}/{endpoint}/"
29+
method: "POST"
30+
response:
31+
status_code: 404
32+
- name: Create method users even with permissions
33+
request:
34+
url: "{api_live_url}/{endpoint}/"
35+
headers:
36+
Authorization: "Token {token_user_can_add}"
37+
method: "POST"
38+
response:
39+
status_code: 404
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
test_name: Delete is disabled
2+
3+
includes:
4+
- !include schema.yml
5+
6+
marks:
7+
- usefixtures:
8+
- api_live_url
9+
- report_photo
10+
- token_user_can_delete
11+
12+
stages:
13+
- id: signup
14+
type: ref
15+
- id: login
16+
type: ref
17+
- name: Delete is disabeld for mobile users
18+
request:
19+
url: "{api_live_url}/{endpoint}/{report_photo.uuid}/"
20+
headers:
21+
Authorization: "Bearer {token}"
22+
method: "DELETE"
23+
response:
24+
status_code: 403
25+
- name: Delete method non authenticated users
26+
request:
27+
url: "{api_live_url}/{endpoint}/{report_photo.uuid}/"
28+
method: "DELETE"
29+
response:
30+
status_code: 403
31+
- name: Delete method authenticated even users with permissions
32+
request:
33+
url: "{api_live_url}/{endpoint}/{report_photo.uuid}/"
34+
headers:
35+
Authorization: "Token {token_user_can_delete}"
36+
method: "DELETE"
37+
response:
38+
status_code: 405
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
3+
test_name: Photos can be read only by authenticated users.
4+
5+
includes:
6+
- !include schema.yml
7+
8+
marks:
9+
- usefixtures:
10+
- api_live_url
11+
- report_photo
12+
- token_user
13+
14+
stages:
15+
- id: signup
16+
type: ref
17+
- id: login
18+
type: ref
19+
- name: Retrieve is not allowed for mobile users
20+
request:
21+
url: "{api_live_url}/{endpoint}/{report_photo.uuid}/"
22+
headers:
23+
Authorization: "Bearer {token}"
24+
method: "GET"
25+
response:
26+
status_code: 403
27+
- name: Non auth user can not retrieve
28+
request:
29+
url: "{api_live_url}/{endpoint}/{report_photo.uuid}/"
30+
method: "GET"
31+
response:
32+
status_code: 403
33+
- name: User without perm view can retrieve
34+
request:
35+
url: "{api_live_url}/{endpoint}/{report_photo.uuid}/"
36+
method: "GET"
37+
headers:
38+
Authorization: "Token {token_user:s}"
39+
response:
40+
status_code: 200
41+
json: !force_format_include "{response_data_validation}"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
test_name: Photos can not be listed.
2+
3+
includes:
4+
- !include schema.yml
5+
6+
marks:
7+
- usefixtures:
8+
- api_live_url
9+
- token_user_can_view
10+
11+
stages:
12+
- id: signup
13+
type: ref
14+
- id: login
15+
type: ref
16+
- name: List method returns not exist for mobile users
17+
request:
18+
url: "{api_live_url}/{endpoint}/"
19+
headers:
20+
Authorization: "Bearer {token}"
21+
method: "GET"
22+
response:
23+
status_code: 404
24+
- name: Non auth user can not list
25+
request:
26+
url: "{api_live_url}/{endpoint}/"
27+
method: "GET"
28+
response:
29+
status_code: 404
30+
- name: Auth user can not list even with permission
31+
request:
32+
url: "{api_live_url}/{endpoint}/"
33+
method: "GET"
34+
headers:
35+
Authorization: "Token {token_user_can_view:s}"
36+
response:
37+
status_code: 404
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
3+
name: Common test information
4+
description: Login information for test server
5+
6+
variables:
7+
endpoint: "photos"
8+
response_data_validation:
9+
uuid: !anystr
10+
image_url: !anystr
11+
image_path: !anystr
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
test_name: Update is disabled
2+
3+
includes:
4+
- !include schema.yml
5+
6+
marks:
7+
- usefixtures:
8+
- api_live_url
9+
- report_photo
10+
- token_user_can_change
11+
- parametrize:
12+
key: method
13+
vals:
14+
- PUT
15+
- PATCH
16+
17+
stages:
18+
- id: signup
19+
type: ref
20+
- id: login
21+
type: ref
22+
- name: Update is disabeld for mobile users
23+
request:
24+
url: "{api_live_url}/{endpoint}/{report_photo.uuid}/"
25+
headers:
26+
Authorization: "Bearer {token}"
27+
method: "{method}"
28+
response:
29+
status_code: 403
30+
- name: Update is disabeld for non auth users
31+
request:
32+
url: "{api_live_url}/{endpoint}/{report_photo.uuid}/"
33+
method: "{method}"
34+
response:
35+
status_code: 403
36+
- name: Update is disabeld users even with permissions
37+
request:
38+
url: "{api_live_url}/{endpoint}/{report_photo.uuid}/"
39+
headers:
40+
Authorization: "Token {token_user_can_change}"
41+
method: "{method}"
42+
response:
43+
status_code: 405

api/tests/integration/reports/list.tavern.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ stages:
218218
strict:
219219
- json:off
220220
json:
221-
count: 0
221+
count: 1
222222
- name: Filter by has_photos False
223223
request:
224224
url: "{api_live_url}/{endpoint}/?has_photos=false"
@@ -228,4 +228,4 @@ stages:
228228
strict:
229229
- json:off
230230
json:
231-
count: 1
231+
count: 0

0 commit comments

Comments
 (0)