1
+ import pytest
1
2
from django .core .files .uploadedfile import SimpleUploadedFile
3
+ from rest_framework .exceptions import ValidationError
2
4
3
5
from alexandria .core import api
4
6
from alexandria .core .factories import FileData
7
+ from alexandria .core .models import File
5
8
6
9
7
10
def test_create_document_file (db , category ):
@@ -21,3 +24,100 @@ def test_create_document_file(db, category):
21
24
)
22
25
assert doc .title == "Bar.pdf"
23
26
assert file .name == "Mee.pdf"
27
+
28
+
29
+ @pytest .mark .parametrize (
30
+ "same_category" ,
31
+ [
32
+ True ,
33
+ False ,
34
+ ],
35
+ )
36
+ def test_copy_document_api (db , category , category_factory , same_category ):
37
+ # initial document with one file
38
+ input_doc , first_file = api .create_document_file (
39
+ "Foo" ,
40
+ "Baz" ,
41
+ category ,
42
+ "Bar.pdf" ,
43
+ "Mee.pdf" ,
44
+ SimpleUploadedFile (
45
+ name = "test.png" ,
46
+ content = FileData .png ,
47
+ content_type = "png" ,
48
+ ),
49
+ "image/png" ,
50
+ 1 ,
51
+ )
52
+
53
+ # add an extra file to the document
54
+ extra_file = api .create_file (
55
+ input_doc ,
56
+ "Foo2" ,
57
+ "Baz2" ,
58
+ "Mee2.pdf" ,
59
+ SimpleUploadedFile (
60
+ name = "test2.jpg" ,
61
+ content = FileData .png ,
62
+ content_type = "jpg" ,
63
+ ),
64
+ "image/jpeg" ,
65
+ 2 ,
66
+ )
67
+
68
+ to_category = category if same_category else category_factory ()
69
+ copied_doc = api .copy_document (input_doc , "CopyUser" , "CopyGroup" , to_category )
70
+ files = copied_doc .files .order_by ("variant" , "created_at" )
71
+
72
+ assert copied_doc .title == "Bar (copy).pdf"
73
+ assert copied_doc .category .pk == to_category .pk
74
+ # document copy will have the user/group of the user who copied it
75
+ assert copied_doc .created_by_user == "CopyUser"
76
+ assert copied_doc .created_by_group == "CopyGroup"
77
+
78
+ # 2 copied files + 2 new thumbnails
79
+ assert len (files ) == 4
80
+
81
+ # original 1
82
+ assert first_file .pk != files [0 ].pk
83
+ assert files [0 ].document .pk == copied_doc .pk
84
+ assert files [0 ].variant == File .Variant .ORIGINAL
85
+ assert files [0 ].name == "Mee.pdf"
86
+ assert files [0 ].mime_type == "image/png"
87
+ assert files [0 ].size == 1
88
+ # files will retain the user/group of the original document
89
+ assert files [0 ].created_by_user == "Foo"
90
+ assert files [0 ].created_by_group == "Baz"
91
+
92
+ # new thumbnail for first file
93
+ assert files [2 ].document .pk == copied_doc .pk
94
+ assert files [2 ].variant == File .Variant .THUMBNAIL
95
+
96
+ # original 2
97
+ assert extra_file .pk != files [1 ].pk
98
+ assert files [1 ].document .pk == copied_doc .pk
99
+ assert files [1 ].variant == File .Variant .ORIGINAL
100
+ assert files [1 ].name == "Mee2.pdf"
101
+ assert files [1 ].mime_type == "image/jpeg"
102
+ assert files [1 ].size == 2
103
+ # files will retain the user/group of the original document
104
+ assert files [1 ].created_by_user == "Foo2"
105
+ assert files [1 ].created_by_group == "Baz2"
106
+
107
+ # new thumbnail for extra file
108
+ assert files [3 ].document .pk == copied_doc .pk
109
+ assert files [3 ].variant == File .Variant .THUMBNAIL
110
+
111
+
112
+ def test_presigning_api (db , file ):
113
+ _ , expires , signature = api .make_signature_components (
114
+ file .pk , "testserver" , download_path = "/foo"
115
+ )
116
+
117
+ api .verify_signed_components (
118
+ file .pk , "testserver" , signature , expires , download_path = "/foo"
119
+ )
120
+ with pytest .raises (ValidationError ):
121
+ api .verify_signed_components (
122
+ file .pk , "testserver" , "incorrect-signature" , expires , download_path = "/foo"
123
+ )
0 commit comments