source: main/trunk/openPLM/plmapp/tests/views.py @ 474

Revision 474, 30.0 KB checked in by pcosquer, 8 years ago (diff)

tests: add a custom test runner

  • always clean up the xapian database
  • tasks: raise exceptions
Line 
1############################################################################
2# openPLM - open source PLM
3# Copyright 2010 Philippe Joulaud, Pierre Cosquer
4#
5# This file is part of openPLM.
6#
7#    openPLM is free software: you can redistribute it and/or modify
8#    it under the terms of the GNU General Public License as published by
9#    the Free Software Foundation, either version 3 of the License, or
10#    (at your option) any later version.
11#
12#    openPLM is distributed in the hope that it will be useful,
13#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15#    GNU General Public License for more details.
16#
17#    You should have received a copy of the GNU General Public License
18#    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
19#
20# Contact :
21#    Philippe Joulaud : ninoo.fr@gmail.com
22#    Pierre Cosquer : pierre.cosquer@insa-rennes.fr
23################################################################################
24
25"""
26This module contains some tests for openPLM.
27"""
28from django.contrib.auth.models import User
29from django.test import TestCase
30
31from openPLM.plmapp import forms
32from openPLM.plmapp.utils import level_to_sign_str
33import openPLM.plmapp.models as m
34from openPLM.plmapp.controllers import DocumentController, PartController, \
35        UserController
36from openPLM.plmapp.lifecycle import LifecycleList
37
38from openPLM.plmapp.tests.base import BaseTestCase
39       
40class CommonViewTest(BaseTestCase):
41    TYPE = "Part"
42    CONTROLLER = PartController
43    DATA = {}
44    REFERENCE = "Part1"
45
46    def setUp(self):
47        super(CommonViewTest, self).setUp()
48        self.client.post("/login/", {'username' : 'user', 'password' : 'password'})
49        self.controller = self.CONTROLLER.create(self.REFERENCE, self.TYPE, "a",
50                                                 self.user, self.DATA)
51        self.base_url = "/object/%s/%s/%s/" % (self.controller.type,
52                                              self.controller.reference,
53                                              self.controller.revision)
54        brian = User.objects.create(username="Brian", password="life")
55        brian.get_profile().is_contributor = True
56        brian.get_profile().save()
57        self.brian = brian
58
59    def post(self, url, data=None, follow=True, status_code=200,
60            link=False, page=""):
61        return self.get_or_post(self.client.post, url, data, follow, status_code,
62                link, page)
63
64    def get(self, url, data=None, follow=True, status_code=200,
65            link=False, page=""):
66        return self.get_or_post(self.client.get, url, data, follow, status_code,
67                link, page)
68   
69    def get_or_post(self, func, url, data=None, follow=True, status_code=200,
70            link=False, page=""):
71        response = func(url, data or {}, follow=follow)
72        self.assertEqual(response.status_code, status_code)
73        self.assertEqual(link, response.context["link_creation"])
74        if page:
75            self.assertEqual(page, response.context["current_page"])
76        return response
77
78
79class ViewTest(CommonViewTest):
80
81    def test_home(self):
82        response = self.get("/home/")
83       
84    def test_create_get(self):
85        response = self.get("/object/create/", {"type" : self.TYPE})
86        self.assertEqual(response.context["object_type"], self.TYPE)
87        self.failUnless(response.context["creation_form"])
88   
89    def test_create_post(self):
90        data = self.DATA.copy()
91        data.update({
92                "type" : self.TYPE,
93                "reference" : "mapart",
94                "revision" : "a",
95                "name" : "MaPart",
96                "group" : str(self.group.id),
97                "lifecycle" : m.get_default_lifecycle().pk,
98                "state" : m.get_default_state().pk,
99                })
100
101        response = self.post("/object/create/", data, page="attributes")
102        obj = m.PLMObject.objects.get(type=self.TYPE, reference="mapart", revision="a")
103        self.assertEqual(obj.id, response.context["obj"].id)
104        self.assertEqual("MaPart", obj.name)
105        self.assertEqual(self.user, obj.owner)
106        self.assertEqual(self.user, obj.creator)
107
108    def test_display_attributes(self):
109        response = self.get(self.base_url + "attributes/", page="attributes")
110        self.failUnless(response.context["object_attributes"])
111        attributes = dict((x.capitalize(), y) for (x,y) in
112                          response.context["object_attributes"])
113        # name : empty value
114        self.assertEqual(attributes["Name"], "")
115        # owner and creator : self.user
116        self.assertEqual(attributes["Owner"], self.user)
117        self.assertEqual(attributes["Creator"], self.user)
118
119    def test_edit_attributes(self):
120        data = self.DATA.copy()
121        data.update(type=self.TYPE, name="new_name")
122        response = self.post(self.base_url + "modify/", data)
123        obj = m.get_all_plmobjects()[self.TYPE].objects.all()[0]
124        self.assertEqual(obj.name, data["name"])
125
126    def test_lifecycle(self):
127        response = self.get(self.base_url + "lifecycle/")
128        lifecycles = tuple(response.context["object_lifecycle"])
129        wanted = (("draft", True), ("official", False), ("deprecated", False))
130        self.assertEqual(lifecycles, wanted)
131        # promote
132        response = self.post(self.base_url + "lifecycle/",
133                            {"action" : "PROMOTE"})
134        lifecycles = tuple(response.context["object_lifecycle"])
135        wanted = (("draft", False), ("official", True), ("deprecated", False))
136        self.assertEqual(lifecycles, wanted)
137        # demote
138        lcl = LifecycleList("diop", "official", "draft",
139                "issue1", "official", "deprecated")
140        lc = m.Lifecycle.from_lifecyclelist(lcl)
141        self.controller.lifecycle = lc
142        self.controller.state = m.State.objects.get(name="draft")
143        self.controller.save()
144        self.controller.promote()
145        self.assertEqual(self.controller.state.name, "issue1")
146        response = self.post(self.base_url + "lifecycle/",
147                                    {"action" : "DEMOTE"})
148        lifecycles = tuple(response.context["object_lifecycle"])
149        wanted = (("draft", True), ("issue1", False), ("official", False),
150                ("deprecated", False))
151        self.assertEqual(lifecycles, wanted)
152
153    def test_revisions(self):
154        response = self.get(self.base_url + "revisions/")
155        revisions = response.context["revisions"]
156        self.assertEqual(revisions, [self.controller.object])
157        # check add_revision_form
158        add_revision_form = response.context["add_revision_form"]
159        self.assertEqual(add_revision_form.data, {"revision": "b"})
160        response = self.post(self.base_url + "revisions/", {"revision" :"b"})
161        m.get_all_plmobjects()[self.TYPE].objects.get(reference=self.controller.reference,
162                revision="b")
163   
164    def test_history(self):
165        response = self.get(self.base_url + "history/")
166
167    def test_navigate_get(self):
168        response = self.get(self.base_url + "navigate/")
169        self.assertTrue(response.context["filter_object_form"])
170        self.assertTrue(response.context["navigate_bool"])
171       
172    def test_navigate_post(self):
173        data = dict.fromkeys(("child", "parents",
174            "doc", "parents", "owner", "signer", "notified", "part",
175            "ownede", "to_sign", "request_notification_from"), True)
176        data["prog"] = "neato"
177        response = self.post(self.base_url + "navigate/", data)
178        self.assertTrue(response.context["filter_object_form"])
179       
180    def test_management(self):
181        response = self.get(self.base_url + "management/", page="management")
182        self.controller.set_owner(self.brian)
183        response = self.get(self.base_url + "management/")
184        self.assertFalse(response.context["is_notified"])
185        form = response.context["notify_self_form"]
186        self.assertEqual("User", form.initial["type"])
187        self.assertEqual(self.user.username, form.initial["username"])
188
189    def test_management_add_get(self):
190        response = self.get(self.base_url + "management/add/",
191               link=True, page="management")
192        attach = response.context["attach"]
193        self.assertEqual(self.controller.id, attach[0].id)
194        self.assertEqual("delegate", attach[1])
195
196    def test_management_add_post(self):
197        data = dict(type="User", username=self.brian.username)
198        response = self.post(self.base_url + "management/add/", data)
199        self.assertTrue(m.PLMObjectUserLink.objects.filter(plmobject=self.controller.object,
200            user=self.brian, role=m.ROLE_NOTIFIED))
201
202    def test_management_replace_get(self):
203        role = level_to_sign_str(0)
204        self.controller.set_signer(self.brian, role)
205        link = m.PLMObjectUserLink.objects.get(plmobject=self.controller.object,
206            user=self.brian, role=role)
207        response = self.get(self.base_url + "management/replace/%d/" % link.id,
208                link=True, page="management")
209        attach = response.context["attach"]
210        self.assertEqual(self.controller.id, attach[0].id)
211        self.assertEqual("delegate", attach[1])
212   
213    def test_management_replace_post(self):
214        role = level_to_sign_str(0)
215        self.controller.set_signer(self.brian, role)
216        link = m.PLMObjectUserLink.objects.get(plmobject=self.controller.object,
217            user=self.brian, role=role)
218        data = dict(type="User", username=self.user.username)
219        response = self.post(self.base_url + "management/replace/%d/" % link.id,
220                        data)
221        self.assertFalse(m.PLMObjectUserLink.objects.filter(plmobject=self.controller.object,
222            user=self.brian, role=role))
223        self.assertTrue(m.PLMObjectUserLink.objects.filter(plmobject=self.controller.object,
224            user=self.user, role=role))
225
226    def test_management_delete(self):
227        self.controller.add_notified(self.brian)
228        link = m.PLMObjectUserLink.objects.get(plmobject=self.controller.object,
229            user=self.brian, role=m.ROLE_NOTIFIED)
230        data = {"link_id" : link.id }
231        response = self.post(self.base_url + "management/delete/", data)
232        self.assertFalse(m.PLMObjectUserLink.objects.filter(plmobject=self.controller.object,
233            user=self.brian, role=m.ROLE_NOTIFIED))
234
235
236class DocumentViewTestCase(ViewTest):
237
238    TYPE = "Document"
239    CONTROLLER = DocumentController
240
241    def test_related_parts_get(self):
242        part = PartController.create("RefPart", "Part", "a", self.user, self.DATA)
243        self.controller.attach_to_part(part)
244       
245        response = self.get(self.base_url + "parts/", page="parts")
246        self.assertEqual([part.id],
247                         [p.part.id for p in response.context["object_rel_part"]])
248       
249    def test_add_related_part_get(self):
250        response = self.get(self.base_url + "parts/add/", link=True)
251        self.assertTrue(isinstance(response.context["add_rel_part_form"],
252                                   forms.AddRelPartForm))
253        attach = response.context["attach"]
254        self.assertEqual(self.controller.id, attach[0].id)
255        self.assertEqual("attach_part", attach[1])
256
257    def test_add_related_part_post(self):
258        part = PartController.create("RefPart", "Part", "a", self.user, self.DATA)
259        data = {
260                "reference" : part.reference,
261                "type" : part.type,
262                "revision" : part.revision
263                }
264        response = self.post(self.base_url + "parts/add/", data)
265        self.assertEqual([part.id],
266                         [p.part.id for p in self.controller.get_attached_parts()])
267
268    def test_files_empty_get(self):
269        response = self.get(self.base_url + "files/", page="files")
270        formset = response.context["file_formset"]
271        self.assertEqual(0, formset.total_form_count())
272
273    def test_files_get(self):
274        self.controller.add_file(self.get_file())
275        response = self.get(self.base_url + "files/", page="files")
276        formset = response.context["file_formset"]
277        self.assertEqual(1, formset.total_form_count())
278       
279    def test_files_post(self):
280        df1 = self.controller.add_file(self.get_file())
281        df2 = self.controller.add_file(self.get_file())
282        data = {
283                'form-0-document': self.controller.id,
284                'form-0-id': df1.id,
285                'form-0-delete' : 'on',
286                'form-0-ORDER': '0',
287                'form-1-document': self.controller.id,
288                'form-1-id': df2.id,
289                'form-1-ORDER': '1',
290                'form-MAX_NUM_FORMS': '',
291                'form-TOTAL_FORMS': 2,
292                'form-INITIAL_FORMS': 2,
293                }
294        response = self.post(self.base_url + "files/", data)
295        self.assertEqual([df2.id], [df.id for df in self.controller.files])
296
297    def test_add_file_get(self):
298        response = self.get(self.base_url + "files/add/")
299        self.assertTrue(isinstance(response.context["add_file_form"],
300                                   forms.AddFileForm))
301
302    def test_add_file_post(self):
303        f = self.get_file(data="crumble")
304        data = { "filename" : f }
305        response = self.post(self.base_url + "files/add/", data)
306        df = self.controller.files[0]
307        self.assertEqual(df.filename, f.name)
308        self.assertEqual("crumble", df.file.read())
309
310    def test_lifecycle(self):
311        self.controller.add_file(self.get_file())
312        super(DocumentViewTestCase, self).test_lifecycle()
313
314class PartViewTestCase(ViewTest):
315
316    def test_children(self):
317        child1 = PartController.create("c1", "Part", "a", self.user, self.DATA)
318        self.controller.add_child(child1, 10 , 20)
319        child2 = PartController.create("c2", "Part", "a", self.user, self.DATA)
320        self.controller.add_child(child2, 10, 20)
321        response = self.get(self.base_url + "BOM-child/", page="BOM-child")
322        self.assertEqual(2, len(list(response.context["children"])))
323        form = response.context["display_form"]
324
325    def test_add_child(self):
326        response = self.get(self.base_url + "BOM-child/add/", link=True)
327        child1 = PartController.create("c1", "Part", "a", self.user, self.DATA)
328        response = self.client.post(self.base_url + "BOM-child/add/",
329                {"type": "Part", "reference":"c1", "revision":"a",
330                 "quantity" : 10, "order" : 10 })
331        self.assertEquals(1, len(self.controller.get_children()))
332
333    def test_edit_children(self):
334        child1 = PartController.create("c1", "Part", "a", self.user, self.DATA)
335        self.controller.add_child(child1, 10 , 20)
336        response = self.get(self.base_url + "BOM-child/edit/")
337        formset = response.context["children_formset"]
338        data = {
339            'form-TOTAL_FORMS': u'1',
340            'form-INITIAL_FORMS': u'1',
341            'form-MAX_NUM_FORMS': u'',
342            'form-0-child' :   child1.id,
343            'form-0-id'  : self.controller.get_children()[0].link.id,
344            'form-0-order'  :  45,
345            'form-0-parent' :  self.controller.id,
346            'form-0-quantity' :  '45.0',
347        }
348        response = self.post(self.base_url + "BOM-child/edit/", data)
349        link = self.controller.get_children()[0].link
350        self.assertEquals(45, link.order)
351        self.assertEquals(45.0, link.quantity)
352
353    def test_parents_empty(self):
354        response = self.get(self.base_url + "parents/", page="parents")
355        self.assertEqual(0, len(list(response.context["parents"])))
356       
357    def test_parents(self):
358        p1 = PartController.create("c1", "Part", "a", self.user, self.DATA)
359        p1.add_child(self.controller, 10, 20)
360        p2 = PartController.create("c2", "Part", "a", self.user, self.DATA)
361        p2.add_child(self.controller, 10, 20)
362        response = self.get(self.base_url + "parents/", page="parents")
363        self.assertEqual(2, len(list(response.context["parents"])))
364
365    def test_doc_cad_empty(self):
366        response = self.get(self.base_url + "doc-cad/", page="doc-cad")
367        self.assertEqual(0, len(list(response.context["object_doc_cad"])))
368   
369    def test_doc_cad(self):
370        doc1 = DocumentController.create("doc1", "Document", "a", self.user,
371                self.DATA)
372        doc2 = DocumentController.create("doc2", "Document", "a", self.user,
373                self.DATA)
374        self.controller.attach_to_document(doc1)
375        self.controller.attach_to_document(doc2)
376        response = self.get(self.base_url + "doc-cad/", page="doc-cad")
377        self.assertEqual(2, len(list(response.context["object_doc_cad"])))
378
379    def test_doc_add_add_get(self):
380        response = self.get(self.base_url + "doc-cad/add/", link=True)
381        self.assertEqual("attach_doc", response.context["attach"][1])
382
383    def test_doc_add_add_post(self):
384        doc1 = DocumentController.create("doc1", "Document", "a", self.user,
385                self.DATA)
386        data = {"type" : doc1.type, "reference" : doc1.reference,
387                "revision" : doc1.revision }
388        response = self.post(self.base_url + "doc-cad/add/", data)
389        document = self.controller.get_attached_documents()[0].document
390        self.assertEqual(doc1.object, document)
391       
392
393class UserViewTestCase(CommonViewTest):
394
395    def setUp(self):
396        super(UserViewTestCase, self).setUp()
397        self.user_url = "/user/%s/" % self.user.username
398        self.controller = UserController(self.user, self.user)
399       
400    def test_user_attribute(self):
401        response = self.get(self.user_url + "attributes/", page="attributes")
402        attributes = dict((x.capitalize(), y) for (x,y) in
403                          response.context["object_attributes"])
404        self.assertEqual(attributes["E-mail address"], self.user.email)
405        self.assertTrue(response.context["is_owner"])
406
407    def test_groups(self):
408        response = self.get(self.user_url + "groups/")
409        # TODO
410
411    def test_part_doc_cads(self):
412        response = self.get(self.user_url + "parts-doc-cad/")
413        # TODO
414       
415    def test_history(self):
416        response = self.get(self.user_url + "history/")
417       
418    def test_navigate(self):
419        response = self.get(self.user_url + "navigate/")
420
421    def test_sponsor_get(self):
422        response = self.get(self.user_url + "delegation/sponsor/")
423        form = response.context["sponsor_form"]
424        self.assertEquals(set(g.id for g in self.user.groupinfo_owner.all()),
425                set(g.id for g in form.fields["groups"].queryset.all()))
426
427    def test_sponsor_post(self):
428        data = dict(sponsor=self.user.id,
429                    username="loser", first_name="You", last_name="Lost",
430                    email="you.lost@example.com", groups=[self.group.pk])
431        response = self.post(self.user_url + "delegation/sponsor/", data)
432        user = User.objects.get(username=data["username"])
433        for attr in ("first_name", "last_name", "email"):
434            self.assertEquals(data[attr], getattr(user, attr))
435        self.assertTrue(user.get_profile().is_contributor)
436        self.assertFalse(user.get_profile().is_administrator)
437        self.assertTrue(user.groups.filter(id=self.group.id))
438
439    def test_modify_get(self):
440        response = self.get(self.user_url + "modify/")
441        form = response.context["modification_form"]
442        self.assertEqual(self.user.first_name, form.initial["first_name"])
443        self.assertEqual(self.user.email, form.initial["email"])
444
445    def test_modify_post(self):
446        data = {"last_name":"Snow", "email":"user@test.com", "first_name":"John"}
447        response = self.post(self.user_url + "modify/", data)
448        user = User.objects.get(username=self.user.username)
449        self.assertEqual("Snow", user.last_name)
450
451    def test_password_get(self):
452        response = self.get(self.user_url + "password/")
453        self.assertTrue(response.context["modification_form"])
454
455    def test_password_post(self):
456        data = dict(old_password="password", new_password1="pw",
457                new_password2="pw")
458        response = self.post(self.user_url + "password/", data)
459        self.user = User.objects.get(pk=self.user.pk)
460        self.assertTrue(self.user.check_password("pw"))
461
462    def test_password_error(self):
463        data = dict(old_password="error", new_password1="pw",
464                new_password2="pw")
465        response = self.post(self.user_url + "password/", data)
466        self.user = User.objects.get(pk=self.user.pk)
467        self.assertTrue(self.user.check_password("password"))
468        self.assertFalse(self.user.check_password("pw"))
469
470    def test_delegation_get(self):
471        response = self.get(self.user_url + "delegation/")
472       
473    def test_delegation_remove(self):
474        self.controller.delegate(self.brian, m.ROLE_OWNER)
475        link = self.controller.get_user_delegation_links()[0]
476        data = {"link_id" : link.id }
477        response = self.post(self.user_url + "delegation/", data)
478        self.assertFalse(self.controller.get_user_delegation_links())
479       
480    def test_delegate_get(self):
481        for role in ("owner", "notified"):
482            url = self.user_url + "delegation/delegate/%s/" % role
483            response = self.get(url, link=True, page="delegation")
484            self.assertEqual(role, unicode(response.context["role"]))
485   
486    def test_delegate_sign_get(self):
487        for level in ("all", "1", "2"):
488            url = self.user_url + "delegation/delegate/sign/%s/" % str(level)
489            response = self.get(url, link=True, page="delegation")
490            role = unicode(response.context["role"])
491            self.assertTrue(role.startswith("signer"))
492            self.assertTrue(level in role)
493
494    def test_delegate_post(self):
495        data = { "type" : "User", "username": self.brian.username }
496        for role in ("owner", "notified"):
497            url = self.user_url + "delegation/delegate/%s/" % role
498            response = self.post(url, data)
499            m.DelegationLink.objects.get(role=role, delegator=self.user,
500                    delegatee=self.brian)
501
502    def test_delegate_sign_post(self):
503        data = { "type" : "User", "username": self.brian.username }
504        for level in xrange(1, 4):
505            url = self.user_url + "delegation/delegate/sign/%d/" % level
506            response = self.post(url, data)
507            role = level_to_sign_str(level - 1)
508            m.DelegationLink.objects.get(role=role,
509                delegator=self.user, delegatee=self.brian)
510
511    def test_delegate_sign_all_post(self):
512        # sign all level
513        data = { "type" : "User", "username": self.brian.username }
514        url = self.user_url + "delegation/delegate/sign/all/"
515        response = self.post(url, data)
516        for level in xrange(2):
517            role = level_to_sign_str(level)
518            m.DelegationLink.objects.get(role=role, delegator=self.user,
519                    delegatee=self.brian)
520   
521def sorted_objects(l):
522    return sorted(l, key=lambda x: x.id)
523
524from django.core.management import call_command
525class SearchViewTestCase(CommonViewTest):
526
527    def get_query(self, request):
528        if isinstance(request, basestring):
529            return request
530        query = u" ".join(u"%s:%s" % q for q in request.iteritems() if q[0] != "type")
531        query = query or "*"
532        return query
533
534    def search(self, request, type=None):
535        # rebuild the index
536        call_command("rebuild_index", interactive=False, verbosity=0)
537       
538        query = self.get_query(request)
539        t = type or request["type"]
540        response = self.get("/user/user/attributes/",
541                {"type" : t, "q" : query})
542        results = list(response.context["results"])
543        results.sort(key=lambda r:r.object.pk)
544        return [r.object for r in results]
545
546    def test_forms(self):
547        response = self.get("/user/user/attributes/")
548        # check if searchforms are present
549        af = response.context["attributes_form"]
550        eaf = response.context["type_form"]
551   
552    def test_session_forms(self):
553        "Tests if form field are kept between two search"
554        data =  {"type" : "Part", "revision" : "c", "name" : "a name"}
555        self.search(data)
556        query = self.get_query(data)
557        for x in range(4):
558            response = self.get("/user/user/attributes/")
559            af = response.context["attributes_form"]
560            self.assertEqual(af.data["q"], query)
561
562    def test_empty(self):
563        "Test a search with an empty database"
564        # clear all plmobject so results is empty
565        for obj in m.PLMObject.objects.all():
566            obj.delete()
567        results = self.search({"type" : self.TYPE})
568        self.assertEqual(results, [])
569
570    def test_one_result(self):
571        "Test a search with one object in the database"
572        results = self.search({"type" : self.TYPE})
573        self.assertEqual(results, [self.controller.object])
574
575    def test_plmobject(self):
576        # add a plmobject : the search should return the same results
577        m.PLMObject.objects.create(reference="aa", type="PLMObject",
578                                     revision="c", owner=self.user,
579                                     creator=self.user, group=self.group)
580        results = self.search({"type" : self.TYPE})
581        self.assertEqual(results, [self.controller.object])
582   
583    def test_option_revision(self):
584        # search with more options : revision
585        results = self.search({"type" : self.TYPE,
586                               "revision" : self.controller.revision})
587        self.assertEqual(results, [self.controller.object])
588        results = self.search({"type" : self.TYPE, "revision" : "____"})
589        self.assertEqual(results, [])
590
591    def test_option_name(self):
592        # search with more options : name
593        self.controller.name = "blabla"
594        self.controller.save()
595        results = self.search({"type" : self.TYPE,
596                               "name" : self.controller.name})
597        self.assertEqual(results, [self.controller.object])
598        results = self.search({"type" : self.TYPE, "name" : "____"})
599        self.assertEqual(results, [])
600
601    def test_two_results(self):
602        # add another object
603        c2 = self.CONTROLLER.create("b", self.TYPE, "c", self.user, self.DATA)
604        results = self.search({"type" : self.TYPE})
605        c = self.controller
606        self.assertEqual(results, sorted_objects([c.object, c2.object]))
607
608    def test_search_or(self):
609        c2 = self.CONTROLLER.create("value2", self.TYPE, "c", self.user, self.DATA)
610        c3 = self.CONTROLLER.create("value3", self.TYPE, "c", self.user, self.DATA)
611        results = self.search("%s OR %s" % (self.controller.reference, c2.reference),
612                self.TYPE)
613        self.assertEqual(sorted_objects([self.controller.object, c2.object]),
614                         sorted_objects(results))
615
616    def test_search_and(self):
617        c2 = self.CONTROLLER.create("value2", self.TYPE, "c", self.user, self.DATA)
618        c3 = self.CONTROLLER.create("value3", self.TYPE, "c", self.user, self.DATA)
619        results = self.search("%s AND %s" % (self.controller.reference, c2.reference),
620                self.TYPE)
621        self.assertEqual([], results)
622        results = self.search("value2 AND revision:c", self.TYPE)
623        self.assertEqual([c2.object], results)
624
625    def test_search_lisp_is_back(self):
626        c2 = self.CONTROLLER.create("value2", self.TYPE, "c", self.user, self.DATA)
627        c3 = self.CONTROLLER.create("value3", self.TYPE, "c", self.user, self.DATA)
628        results = self.search("((%s) AND (%s) ) OR (*)" % (self.controller.reference,
629            c2.reference), self.TYPE)
630        self.assertEqual(3, len(results))
631
632    def test_search_dash(self):
633        for i in xrange(6):
634            self.CONTROLLER.create("val-0%d" % i, self.TYPE, "c",
635                    self.user, self.DATA)
636       
637        self.CONTROLLER.create("val-0i-5", self.TYPE, "c", self.user, self.DATA)
638        c = self.CONTROLLER.create("0i-5", self.TYPE, "c", self.user, self.DATA)
639        results = self.search("val-0*", self.TYPE)
640        self.assertEqual(7, len(results))
641        self.assertTrue(c.object not in results)
642
643    def test_search_all(self):
644        for i in xrange(6):
645            self.CONTROLLER.create("val-0%d" % i, self.TYPE, "c",
646                    self.user, self.DATA)
647        results = self.search("*", self.TYPE)
648        self.assertEqual(set(m.Part.objects.all()), set(results))
649
650    def test_search_not(self):
651        results = self.search("NOT %s" % self.controller.name, self.TYPE)
652        self.assertEqual([], results)
653        results = self.search("NOT nothing", self.TYPE)
654        self.assertEqual([self.controller.object], results)
655
656
657class MechantUserViewTest(TestCase):
658    """
659    Tests when an user try an unauthorized action
660    """
661
662    TYPE = "Part"
663    CONTROLLER = PartController
664    DATA = {}
665   
666    def setUp(self):
667        owner = User(username="owner")
668        owner.set_password("password")
669        owner.save()
670        owner.get_profile().is_contributor = True
671        owner.get_profile().save()
672        self.user = User(username="user")
673        self.user.set_password("password")
674        self.user.save()
675        self.user.get_profile().is_contributor = True
676        self.user.get_profile().save()
677        self.group = m.GroupInfo(name="grp", owner=self.user, creator=self.user,
678                description="grp")
679        self.group.save()
680        self.user.groups.add(self.group)
681        self.client.post("/login/", {'username' : 'user', 'password' : 'password'})
682        self.controller = self.CONTROLLER.create("Part1", "Part", "a", owner,
683                {"group":self.group})
684        self.base_url = "/object/%s/%s/%s/" % (self.controller.type,
685                                              self.controller.reference,
686                                              self.controller.revision)
687   
688    def test_edit_attributes(self):
689        data = self.DATA.copy()
690        data.update(type=self.TYPE, name="new_name")
691        response = self.client.post(self.base_url + "modify/", data, follow=True)
692        obj = m.get_all_plmobjects()[self.TYPE].objects.all()[0]
693        self.assertEqual(obj.name, '')
694        self.assertEqual(response.template.name, "error.html")
695
696class SpecialCharactersPartViewTestCase(PartViewTestCase):
697    REFERENCE = u"Pa *-\xc5\x93\xc3\xa9'"
698
699
700class SpecialCharactersDocumentViewTestCase(DocumentViewTestCase):
701    REFERENCE = u"Pa *-\xc5\x93\xc3\xa9'"
702
Note: See TracBrowser for help on using the repository browser.