source: main/branches/3D/openPLM/plmapp/tests/api.py @ 662

Revision 662, 12.4 KB checked in by pcosquer, 8 years ago (diff)

3D branch: merge changes from trunk (rev [661])

Line 
1from django.utils.simplejson import JSONDecoder
2from django.core.files.base import ContentFile
3
4from openPLM.plmapp.tests.views import CommonViewTest
5from openPLM.plmapp.controllers.document import DocumentController
6
7
8class ApiTestCase(CommonViewTest):
9
10    def get(self, url, **get):
11        return JSONDecoder().decode(self.client.get(url, get,
12                HTTP_USER_AGENT="openplm").content)
13
14    def post(self, url, **post):
15        return JSONDecoder().decode(self.client.post(url, post,
16            HTTP_USER_AGENT="openplm").content)
17
18    def test_login(self):
19        data = self.post("/api/login/", username="user", password="password")
20        self.assertEqual("ok", data["result"])
21        self.assertEqual("user", data["username"])
22
23    def test_login_error(self):
24        data = self.post("/api/login/", username="user", password="fail")
25        self.assertEqual("error", data["result"])
26
27    def test_get_all_types(self):
28        data = self.get("/api/types/")
29        self.assertEqual("ok", data["result"])
30        self.assertTrue("Part" in data["types"])
31        self.assertTrue("Document" in data["types"])
32
33    def test_get_all_docs(self):
34        data = self.get("/api/docs/")
35        self.assertEqual("ok", data["result"])
36        self.assertFalse("Part" in data["types"])
37        self.assertTrue("Document" in data["types"])
38        self.assertTrue("OfficeDocument" in data["types"])
39   
40    def test_get_all_parts(self):
41        data = self.get("/api/parts/")
42        self.assertEqual("ok", data["result"])
43        self.assertTrue("Part" in data["types"])
44        self.assertFalse("Document" in data["types"])
45        self.assertTrue("SinglePart" in data["types"])
46
47    def test_test_login(self):
48        data = self.get("/api/testlogin/")
49        self.assertEqual("ok", data["result"])
50       
51    def test_search(self):
52        for url in ("/api/search/", "/api/search/false/",
53                "/api/search/false/false/"):
54            data = self.get("/api/search/", type="Part", reference="Pa*")
55            self.assertEqual("ok", data["result"])
56            self.assertEqual(1, len(data["objects"]))
57            part = data["objects"][0]
58            self.assertEqual(self.controller.name, part["name"])
59            self.assertEqual(self.controller.id, part["id"])
60            self.assertEqual(self.controller.revision, part["revision"])
61
62    def test_search_editable_only(self):
63        self.attach_to_official_document()
64        self.controller.promote()
65        data = self.get("/api/search/true/", type="Part", reference="Pa*")
66        self.assertEqual("ok", data["result"])
67        self.assertEqual(0, len(data["objects"]))
68
69    def test_search_error_missing_type(self):
70        data = self.get("/api/search/")
71        self.assertEqual("error", data["result"])
72   
73    def test_search_with_file_only(self):
74        doc = DocumentController.create("Doc", "Document", "a", self.user,
75                self.DATA)
76        data = self.get("/api/search/true/true/", type="Document")
77        self.assertEqual("ok", data["result"])
78        self.assertEqual(0, len(data["objects"]))
79
80        doc.add_file(self.get_file())
81        data = self.get("/api/search/true/true/", type="Document")
82        self.assertEqual("ok", data["result"])
83        self.assertEqual(1, len(data["objects"]))
84       
85        d = data["objects"][0]
86        self.assertEqual(doc.name, d["name"])
87        self.assertEqual(doc.id, d["id"])
88        self.assertEqual(doc.revision, d["revision"])
89
90    def test_get_search_fields_part(self):
91        data = self.get("/api/search_fields/Part/")
92        self.assertEqual("ok", data["result"])
93        fields = data["fields"]
94        name_field = [f for f in fields if f["name"] == "name"][0]
95        self.assertEqual("text", name_field["type"])
96        reference_field = [f for f in fields if f["name"] == "reference"][0]
97        self.assertEqual("text", reference_field["type"])
98   
99    def test_get_search_fields_harddisk(self):
100        data = self.get("/api/search_fields/HardDisk/")
101        self.assertEqual("ok", data["result"])
102        fields = data["fields"]
103        name_field = [f for f in fields if f["name"] == "name"][0]
104        self.assertEqual("text", name_field["type"])
105        capacity_field = [f for f in fields if f["name"] == "capacity_in_go"][0]
106        self.assertEqual("int", capacity_field["type"])
107
108    def test_get_search_unknown_type(self):
109        data = self.get("/api/search_fields/UnknownType/")
110        self.assertEqual("error", data["result"])
111
112    def test_get_creation_fields_part(self):
113        data = self.get("/api/creation_fields/Part/")
114        self.assertEqual("ok", data["result"])
115        fields = data["fields"]
116        name_field = [f for f in fields if f["name"] == "name"][0]
117        self.assertEqual("text", name_field["type"])
118        reference_field = [f for f in fields if f["name"] == "reference"][0]
119        self.assertEqual("text", reference_field["type"])
120        group_field = [f for f in fields if f["name"] == "group"][0]
121        self.assertTrue(group_field["choices"])
122   
123    def test_get_creation_fields_harddisk(self):
124        data = self.get("/api/creation_fields/HardDisk/")
125        self.assertEqual("ok", data["result"])
126        fields = data["fields"]
127        name_field = [f for f in fields if f["name"] == "name"][0]
128        self.assertEqual("text", name_field["type"])
129        capacity_field = [f for f in fields if f["name"] == "capacity_in_go"][0]
130        self.assertEqual("int", capacity_field["type"])
131   
132    def test_get_creation_fields_unknown_type(self):
133        data = self.get("/api/creation_fields/UnknownType/")
134        self.assertEqual("error", data["result"])
135
136    def test_create(self):
137        data = self.post("/api/create/", type="Document", name="docu",
138                reference="DocRef", revision="0", group=self.group.id,
139                lifecycle="draft_official")
140        self.assertEqual("ok", data["result"])
141        obj = data["object"]
142        self.assertEqual("DocRef", obj["reference"])
143
144    def test_create_error_missing_lifecycle(self):
145        data = self.post("/api/create/", type="Document", name="docu",
146                reference="DocRef", revision="0", group=self.group.id)
147        self.assertEqual("error", data["result"])
148
149    def test_next_revision(self):
150        doc = DocumentController.create("Doc", "Document", "a", self.user,
151                self.DATA)
152        data = self.get("/api/object/%d/next_revision/" % doc.id)
153        self.assertEqual("ok", data["result"])
154        self.assertEqual("b", data["revision"])
155
156    def test_is_revisable(self):
157        doc = DocumentController.create("Doc", "Document", "a", self.user,
158                self.DATA)
159        data = self.get("/api/object/%d/is_revisable/" % doc.id)
160        self.assertEqual("ok", data["result"])
161        self.assertEqual(True, data["revisable"])
162
163        doc.revise("b")
164        data = self.get("/api/object/%d/is_revisable/" % doc.id)
165        self.assertEqual("ok", data["result"])
166        self.assertEqual(False, data["revisable"])
167
168    def test_revise(self):
169        doc = DocumentController.create("Doc", "Document", "a", self.user,
170                self.DATA)
171        data = self.post("/api/object/%d/revise/" % doc.id, revision="b")
172        self.assertEqual("ok", data["result"])
173        new_doc = data["doc"]
174        revb = doc.get_next_revisions()[0]
175        self.assertEqual("b", new_doc["revision"])
176        self.assertEqual("b", revb.revision)
177        self.assertEqual(revb.id, new_doc["id"])
178
179    def test_revise_error_not_revisable(self):
180        doc = DocumentController.create("Doc", "Document", "a", self.user,
181                self.DATA)
182        rev = doc.revise("n")
183        data = self.post("/api/object/%d/revise/" % doc.id, revision="b")
184        self.assertEqual("error", data["result"])
185        self.assertEqual([rev.id], [o.id for o in doc.get_next_revisions()])
186
187    def test_revise_error_missing_revision(self):
188        doc = DocumentController.create("Doc", "Document", "a", self.user,
189                self.DATA)
190        data = self.post("/api/object/%d/revise/" % doc.id)
191        self.assertEqual("error", data["result"])
192
193    def test_get_files_empty(self):
194        doc = DocumentController.create("Doc", "Document", "a", self.user,
195                self.DATA)
196        data = self.get("/api/object/%d/files/" % doc.id)
197        self.assertEqual("ok", data["result"])
198        self.assertEqual([], data["files"])
199   
200    def test_get_files(self):
201        doc = DocumentController.create("Doc", "Document", "a", self.user,
202                self.DATA)
203        df = doc.add_file(self.get_file())
204        data = self.get("/api/object/%d/files/" % doc.id)
205        self.assertEqual("ok", data["result"])
206        self.assertEqual([df.id], [f["id"] for f in data["files"]])
207
208    def test_get_files_all(self):
209        doc = DocumentController.create("Doc", "Document", "a", self.user,
210                self.DATA)
211        df = doc.add_file(self.get_file())
212        doc.lock(df)
213        data = self.get("/api/object/%d/files/" % doc.id)
214        self.assertEqual("ok", data["result"])
215        self.assertEqual([], data["files"])
216
217        data = self.get("/api/object/%d/files/all/" % doc.id)
218        self.assertEqual("ok", data["result"])
219        self.assertEqual([df.id], [f["id"] for f in data["files"]])
220   
221    def test_add_file(self):
222        doc = DocumentController.create("Doc", "Document", "a", self.user,
223                self.DATA)
224        mock_file = self.get_file()
225        data = self.post("/api/object/%d/add_file/" % doc.id,
226                filename=mock_file)
227        self.assertEqual("ok", data["result"])
228        self.assertTrue(doc.files)
229        self.assertEqual(mock_file.name, data["doc_file"]["filename"])
230        self.assertEqual(doc.files[0].id, data["doc_file"]["id"])
231       
232    def test_add_file_error_missing_file(self):
233        doc = DocumentController.create("Doc", "Document", "a", self.user,
234                self.DATA)
235        data = self.post("/api/object/%d/add_file/" % doc.id)
236        self.assertEqual("error", data["result"])
237        self.assertFalse(doc.files)
238   
239    def test_attach_to_part(self):
240        doc = DocumentController.create("Doc", "Document", "a", self.user,
241                self.DATA)
242        data = self.get("/api/object/%d/attach_to_part/%d/" %
243                (doc.id, self.controller.id))
244        self.assertEqual("ok", data["result"])
245        self.assertEqual(self.controller.id, doc.get_attached_parts()[0].id)
246
247    def test_is_locked(self):
248        doc = DocumentController.create("Doc", "Document", "a", self.user,
249                self.DATA)
250        df = doc.add_file(self.get_file())
251        data = self.get("/api/object/%d/is_locked/%d/" % (doc.id, df.id))
252        self.assertEqual("ok", data["result"])
253        self.assertFalse(data["locked"])
254
255        doc.lock(df)
256        data = self.get("/api/object/%d/is_locked/%d/" % (doc.id, df.id))
257        self.assertEqual("ok", data["result"])
258        self.assertTrue(data["locked"])
259
260    def test_lock(self):
261        doc = DocumentController.create("Doc", "Document", "a", self.user,
262                self.DATA)
263        df = doc.add_file(self.get_file())
264        data = self.get("/api/object/%d/lock/%d/" % (doc.id, df.id))
265        self.assertEqual("ok", data["result"])
266        self.assertTrue(doc.files[0].locked)
267
268
269    def test_unlock(self):
270        doc = DocumentController.create("Doc", "Document", "a", self.user,
271                self.DATA)
272        df = doc.add_file(self.get_file())
273        doc.lock(df)
274        data = self.get("/api/object/%d/unlock/%d/" % (doc.id, df.id))
275        self.assertEqual("ok", data["result"])
276        self.assertFalse(doc.files[0].locked)
277
278    def test_checkin(self):
279        doc = DocumentController.create("Doc", "Document", "a", self.user,
280                self.DATA)
281        df = doc.add_file(self.get_file())
282        mock_file = self.get_file(data="robert")
283        data = self.post("/api/object/%d/checkin/%d/" % (doc.id, df.id),
284                filename=mock_file)
285        self.assertEqual("ok", data["result"])
286        self.assertEqual("robert", doc.files[0].file.read())
287
288    def test_add_thumbnail(self):
289        doc = DocumentController.create("Doc", "Document", "a", self.user,
290                self.DATA)
291        df = doc.add_file(self.get_file())
292        thumbnail = ContentFile(file("datatests/thumbnail.png").read())
293        thumbnail.name = "Thumbnail.png"
294        data = self.post("/api/object/%d/add_thumbnail/%d/" % (doc.id, df.id),
295                filename=thumbnail)
296        self.assertEqual("ok", data["result"])
297        self.assertNotEqual(None, doc.files[0].thumbnail)
298
299
Note: See TracBrowser for help on using the repository browser.