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

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

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

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