コード例 #1
0
ファイル: sponsor.py プロジェクト: amarh/openPLM
 def handle(self, *args, **options):
     sponsor = User.objects.get(username=options.get('sponsor', None))
     obj = UserController(sponsor, sponsor)
     data = dict(options)
     data["sponsor"] = sponsor.id
     data["warned"] = True
     data["groups"] = list(GroupInfo.objects.filter(name__in=options["groups"]).values_list("id", flat=True))
     form = SponsorForm(data)
     if form.is_valid():
         new_user = form.save()
         new_user.profile.language = form.cleaned_data["language"]
         role = form.cleaned_data["role"]
         obj.sponsor(new_user, role=="contributor", role=="restricted")
     else:
         raise CommandError(form.errors.as_text())
コード例 #2
0
 def test_approve_promotion_two_delegators(self):
     controller = self.create("Part1")
     controller.object.is_promotable = lambda: True
     user = self.get_contributor("gege")
     UserController(self.user, self.user).delegate(user, level_to_sign_str(0))
     user2 = self.get_contributor("misterpink")
     UserController(user2, user2).delegate(user, level_to_sign_str(0))
     models.PLMObjectUserLink.objects.create(user=user2, plmobject=controller.object,
             role=level_to_sign_str(0))
     ctrl2 = self.CONTROLLER(controller.object, user)
     self.assertTrue(ctrl2.is_last_promoter())
     ctrl2.approve_promotion()
     obj = models.PLMObject.objects.get(id=controller.id)
     self.assertEqual(self.cie, obj.owner)
     self.assertEqual("official", obj.state.name)
コード例 #3
0
 def handle(self, *args, **options):
     sponsor = User.objects.get(username=options.get('sponsor', None))
     obj = UserController(sponsor, sponsor)
     data = dict(options)
     data["sponsor"] = sponsor.id
     data["warned"] = True
     data["groups"] = list(
         GroupInfo.objects.filter(name__in=options["groups"]).values_list(
             "id", flat=True))
     form = SponsorForm(data)
     if form.is_valid():
         new_user = form.save()
         new_user.profile.language = form.cleaned_data["language"]
         role = form.cleaned_data["role"]
         obj.sponsor(new_user, role == "contributor", role == "restricted")
     else:
         raise CommandError(form.errors.as_text())
コード例 #4
0
ファイル: user.py プロジェクト: snegovick/openplm
 def test_resend_sponsor_error_not_sponsor(self):
     user = User(username="******", email="*****@*****.**")
     UserController(self.cie, self.cie).sponsor(user)
     link = m.DelegationLink.objects.get(role="sponsor", delegatee=user)
     pwd = user.password
     mail.outbox = []
     self.post(self.user_url + 'delegation/sponsor/mail/',
             {"link_id" : link.id}, status_code=403)
     self.assertEqual(0, len(mail.outbox))
     user = User.objects.get(username="******")
     self.assertEqual(user.password, pwd)
コード例 #5
0
ファイル: csvimport.py プロジェクト: esimorre/openplm
class UsersImporter(CSVImporter):
    """
    A :class:`CSVImporter` that sponsors users from a CSV file.

    The CSV must contain the following columns:

        * username
        * first_name
        * last_name
        * email
        * groups (multiple groups can be separeted by a "/")

    """

    REQUIRED_HEADERS = ('username', 'first_name', 'last_name', 'email', 'groups')

    HEADERS_SET = set(REQUIRED_HEADERS)

    def __init__(self, csv_file, user, encoding="utf-8"):
        self.ctrl = UserController(user, user)
        self.ctrl.block_mails()
        super(UsersImporter, self).__init__(csv_file, user)
        self.groups = dict(user.groups.values_list("name", "id"))

    @classmethod
    def get_headers_set(cls):
        return cls.HEADERS_SET 

    def tear_down(self):
        self.ctrl.unblock_mails()
    
    def parse_row(self, line, row): 
        from openPLM.plmapp.forms import SponsorForm
        un, fn, ln, em, grps = self.get_values(row, *self.REQUIRED_HEADERS)
        groups = []
        for grp in grps.split("/"):
            try:
                groups.append(self.groups[grp])
            except KeyError:
                self.store_errors(line, u"Invalid group:%s" % grp)
                return
        data = {
                "sponsor" : self.user.id,
                "username": un,
                "last_name": ln,
                "first_name": fn,
                "email" : em,
                "groups" : groups,
                "warned" : True,
                }
        form = SponsorForm(data, sponsor=self.user.id)
        if form.is_valid():
            new_user = form.save()
            self.ctrl.sponsor(new_user)
            self.objects.append(new_user)
        else:
            items = (mark_safe(u"%s: %s" % item) for item 
                    in form.errors.iteritems())
            self.store_errors(line, *items)
コード例 #6
0
ファイル: user.py プロジェクト: amarh/openPLM
class UserViewTestCase(CommonViewTest):

    def setUp(self):
        super(UserViewTestCase, self).setUp()
        self.user_url = "/user/%s/" % self.user.username
        self.controller = UserController(self.user, self.user)

    def test_user_attribute(self):
        response = self.get(self.user_url + "attributes/", page="attributes")
        attributes = dict((x.capitalize(), y) for (x, y, z) in
                          response.context["object_attributes"])

        old_lang = translation.get_language()
        translation.activate(self.LANGUAGE)
        key = _("email address")
        translation.activate(old_lang)
        del old_lang

        self.assertEqual(attributes[key.capitalize()], self.user.email)
        self.assertTrue(response.context["is_owner"])

    def test_groups(self):
        response = self.get(self.user_url + "groups/")
        # TODO

    def test_part_doc_cads(self):
        response = self.get(self.user_url + "parts-doc-cad/")
        # TODO

    def test_history(self):
        response = self.get(self.user_url + "history/")

    def test_navigate(self):
        response = self.get(self.user_url + "navigate/")

    def test_sponsor_get(self):
        response = self.get(self.user_url + "delegation/sponsor/")
        form = response.context["sponsor_form"]
        self.assertEquals(set(g.id for g in self.user.groupinfo_owner.all()),
                set(g.id for g in form.fields["groups"].queryset.all()))

    def test_sponsor_post(self):
        data = dict(sponsor=self.user.id,
                    username="******", first_name="You", last_name="Lost",
                    email="*****@*****.**", groups=[self.group.pk],
                    language=self.user.profile.language)
        response = self.post(self.user_url + "delegation/sponsor/", data)
        user = User.objects.get(username=data["username"])
        for attr in ("first_name", "last_name", "email"):
            self.assertEquals(data[attr], getattr(user, attr))
        self.assertTrue(user.profile.is_contributor)
        self.assertFalse(user.profile.is_administrator)
        self.assertTrue(user.groups.filter(id=self.group.id))

    def test_modify_get(self):
        response = self.get(self.user_url + "modify/")
        form = response.context["modification_form"]
        self.assertEqual(self.user.first_name, form.initial["first_name"])
        self.assertEqual(self.user.email, form.initial["email"])

    def test_modify_post(self):
        data = {"last_name":"Snow", "email":"*****@*****.**", "first_name":"John",
                "avatar": "",}
        response = self.post(self.user_url + "modify/", data)
        user = User.objects.get(username=self.user.username)
        self.assertEqual("Snow", user.last_name)

    def test_modify_sponsored_user(self):
        data0 = dict(sponsor=self.user.id,
                    username="******", first_name="You", last_name="Lost",
                    email="*****@*****.**", groups=[self.group.pk],
                    language=self.user.profile.language)
        response = self.post(self.user_url + "delegation/sponsor/", data0)
        data = {"last_name":"Snow", "email":"*****@*****.**", "first_name":"John",
                "avatar":None,}
         # brian can not edit these data
        self.client.login(username=self.brian.username, password="******")
        response = self.client.post("/user/loser/modify/", data)
        user = User.objects.get(username="******")
        self.assertEqual(user.email, data0["email"])
        self.assertEqual(user.first_name, data0["first_name"])
        self.assertEqual(user.last_name, data0["last_name"])

        # self.user can edit these data
        self.client.login(username=self.user.username, password="******")
        response = self.client.post("/user/loser/modify/", data)
        user = User.objects.get(username="******")
        self.assertEqual(user.email, data["email"])
        self.assertEqual(user.first_name, data["first_name"])
        self.assertEqual(user.last_name, data["last_name"])

        # it should not be possible to edit data once loser has logged in
        user.set_password("pwd")
        user.save()
        self.client.login(username=user.username, password="******")
        self.client.get("/home/")
        self.client.login(username=self.user.username, password="******")
        data2 = {"last_name":"Snow2", "email":"*****@*****.**", "first_name":"John2"}
        response = self.client.post("/user/loser/modify/", data2)
        user = User.objects.get(username="******")
        self.assertEqual(user.email, data["email"])
        self.assertEqual(user.first_name, data["first_name"])
        self.assertEqual(user.last_name, data["last_name"])

    def test_password_get(self):
        response = self.get(self.user_url + "password/")
        self.assertTrue(response.context["modification_form"])

    def test_password_post(self):
        data = dict(old_password="******", new_password1="pw",
                new_password2="pw")
        response = self.post(self.user_url + "password/", data)
        self.user = User.objects.get(pk=self.user.pk)
        self.assertTrue(self.user.check_password("pw"))

    def test_password_error(self):
        data = dict(old_password="******", new_password1="pw",
                new_password2="pw")
        response = self.post(self.user_url + "password/", data)
        self.user = User.objects.get(pk=self.user.pk)
        self.assertTrue(self.user.check_password("password"))
        self.assertFalse(self.user.check_password("pw"))

    def test_delegation_get(self):
        response = self.get(self.user_url + "delegation/")

    def test_delegation_remove(self):
        self.controller.delegate(self.brian, m.ROLE_OWNER)
        link = self.controller.get_user_delegation_links()[0]
        data = {"link_id" : link.id }
        response = self.post(self.user_url + "delegation/delete/", data)
        self.assertFalse(self.controller.get_user_delegation_links())

    def test_delegate_get(self):
        for role in ("owner", "notified"):
            url = self.user_url + "delegation/delegate/%s/" % role
            response = self.get(url, link=True, page="delegation")
            self.assertEqual(role, unicode(response.context["role"]))

    def test_delegate_sign_get(self):
        for level in ("all", "1", "2"):
            url = self.user_url + "delegation/delegate/sign/%s/" % str(level)
            response = self.get(url, link=True, page="delegation")
            if self.LANGUAGE == "en":
                role = unicode(response.context["role"])
                self.assertTrue(role.startswith("signer"))
                self.assertTrue(level in role)

    def test_delegate_post(self):
        data = { "type" : "User", "username": self.brian.username }
        for role in ("owner", "notified"):
            url = self.user_url + "delegation/delegate/%s/" % role
            response = self.post(url, data)
            m.DelegationLink.objects.get(role=role, delegator=self.user,
                    delegatee=self.brian)

    def test_delegate_sign_post(self):
        data = { "type" : "User", "username": self.brian.username }
        for level in xrange(1, 4):
            url = self.user_url + "delegation/delegate/sign/%d/" % level
            response = self.post(url, data)
            role = level_to_sign_str(level - 1)
            m.DelegationLink.objects.get(role=role,
                delegator=self.user, delegatee=self.brian)

    def test_delegate_sign_all_post(self):
        # sign all level
        data = { "type" : "User", "username": self.brian.username }
        url = self.user_url + "delegation/delegate/sign/all/"
        response = self.post(url, data)
        for level in xrange(2):
            role = level_to_sign_str(level)
            m.DelegationLink.objects.get(role=role, delegator=self.user,
                    delegatee=self.brian)

    def test_resend_sponsor_mail(self):
        user = User(username="******", email="*****@*****.**")
        self.controller.sponsor(user)
        link = m.DelegationLink.objects.get(role="sponsor", delegatee=user)
        pwd = user.password
        mail.outbox = []
        self.post(self.user_url + 'delegation/sponsor/mail/',
                {"link_id" : link.id})
        self.assertEqual(1, len(mail.outbox))
        self.assertEqual(mail.outbox[0].bcc, [user.email])
        user = User.objects.get(username="******")
        self.assertNotEqual(user.password, pwd)

    def test_resend_sponsor_error_user_connected(self):
        user = User(username="******", email="*****@*****.**")
        self.controller.sponsor(user)
        user.last_login = timezone.now()
        user.save()
        link = m.DelegationLink.objects.get(role="sponsor", delegatee=user)
        pwd = user.password
        mail.outbox = []
        self.post(self.user_url + 'delegation/sponsor/mail/',
                {"link_id" : link.id}, status_code=403)
        self.assertEqual(0, len(mail.outbox))
        user = User.objects.get(username="******")
        self.assertEqual(user.password, pwd)

    def test_resend_sponsor_error_not_sponsor(self):
        user = User(username="******", email="*****@*****.**")
        UserController(self.cie, self.cie).sponsor(user)
        link = m.DelegationLink.objects.get(role="sponsor", delegatee=user)
        pwd = user.password
        mail.outbox = []
        self.post(self.user_url + 'delegation/sponsor/mail/',
                {"link_id" : link.id}, status_code=403)
        self.assertEqual(0, len(mail.outbox))
        user = User.objects.get(username="******")
        self.assertEqual(user.password, pwd)

    def test_upload_file_get(self):
        response = self.get(self.user_url + "files/add/")
        self.assertTrue(isinstance(response.context["add_file_form"],
                                   forms.AddFileForm))

    def test_upload_file_post(self):
        fname = u"toti\xe8o_t.txt"
        name = u"toti\xe8o t"
        f = self.get_file(name=fname, data="crumble")
        data = { "filename" : f }
        response = self.post(self.user_url + "files/add/", data)
        df = list(self.controller.files.all())[0]
        self.assertEquals(df.filename, f.name)
        self.assertEquals("crumble", df.file.read())
        url = "/object/create/?type=Document&pfiles=%d" % df.id
        self.assertRedirects(response, url)
        # post the form as previously returned by "files/add/"
        cform = response.context["creation_form"]
        self.assertEquals(name, cform.initial["name"])
        form = lxml.html.fromstring(response.content).xpath("//form[@id='creation_form']")[0]
        data = dict(form.fields)
        data["template"] = ""
        r2 = self.post(url, data)
        obj = r2.context["obj"]
        self.assertEquals(name, obj.name)
        self.assertEquals(list(obj.files.values_list("filename", flat=True)), [fname])
        self.assertFalse(self.controller.files.all())
        self.assertEquals(obj.files.all()[0].file.read(), "crumble")
コード例 #7
0
ファイル: csvimport.py プロジェクト: smewp/openPLM
 def __init__(self, csv_file, user, encoding="utf-8"):
     self.ctrl = UserController(user, user)
     self.ctrl.block_mails()
     super(UsersImporter, self).__init__(csv_file, user)
     self.groups = dict(user.groups.values_list("name", "id"))
コード例 #8
0
ファイル: tests.py プロジェクト: amarh/openPLM
 def test_calendar_user(self):
     ctrl = UserController(self.user, self.user)
     ctrl.delegate(self.brian, "owner")
     response = self.get("/user/%s/history/calendar/" % self.user.username)
     calendar = response.context["calendar"]
     self.assertTrue("delegation" in calendar)
コード例 #9
0
 def __init__(self, csv_file, user, encoding="utf-8"):
     self.ctrl = UserController(user, user)
     self.ctrl.block_mails()
     super(UsersImporter, self).__init__(csv_file, user)
     self.groups = dict(user.groups.values_list("name", "id"))
コード例 #10
0
class UsersImporter(CSVImporter):
    """
    A :class:`CSVImporter` that sponsors users from a CSV file.

    The CSV must contain the following columns:

        * username
        * first_name
        * last_name
        * email
        * groups (multiple groups can be separeted by a "/")
        * language

    """

    REQUIRED_HEADERS = ('username', 'first_name', 'last_name', 'email', 'groups','language')

    HEADERS_SET = set(REQUIRED_HEADERS)

    def __init__(self, csv_file, user, encoding="utf-8"):
        self.ctrl = UserController(user, user)
        self.ctrl.block_mails()
        super(UsersImporter, self).__init__(csv_file, user)
        self.groups = dict(user.groups.values_list("name", "id"))

    @classmethod
    def get_headers_set(cls):
        return cls.HEADERS_SET

    def tear_down(self):
        self.ctrl.unblock_mails()

    def parse_row(self, line, row):
        from openPLM.plmapp.forms import SponsorForm
        un, fn, ln, em, grps,la = self.get_values(row, *self.REQUIRED_HEADERS)
        groups = []
        for grp in grps.split("/"):
            try:
                groups.append(self.groups[grp])
            except KeyError:
                self.store_errors(line, u"Invalid group:%s" % grp)
                return
        data = {
                "sponsor" : self.user.id,
                "username": un,
                "last_name": ln,
                "first_name": fn,
                "email" : em,
                "groups" : groups,
                "language" : la,
                "warned" : True,
                }
        form = SponsorForm(data, sponsor=self.user.id)
        if form.is_valid():
            new_user = form.save()
            new_user.profile.language = form.cleaned_data["language"]
            self.ctrl.sponsor(new_user)
            self.objects.append(new_user)
        else:
            items = (mark_safe(u"%s: %s" % item) for item
                    in form.errors.iteritems())
            self.store_errors(line, *items)
コード例 #11
0
 def setUp(self):
     super(UserViewTestCase, self).setUp()
     self.user_url = "/user/%s/" % self.user.username
     self.controller = UserController(self.user, self.user)
コード例 #12
0
 def setUp(self):
     super(RestrictedUserControllerTestCase, self).setUp()
     self.ctrl = UserController(self.user, self.user)
     self.rctrl = UserController(self.user, self.restricted_user)
コード例 #13
0
ファイル: views.py プロジェクト: esimorre/openplm
class UserViewTestCase(CommonViewTest):
    def setUp(self):
        super(UserViewTestCase, self).setUp()
        self.user_url = "/user/%s/" % self.user.username
        self.controller = UserController(self.user, self.user)

    def test_user_attribute(self):
        response = self.get(self.user_url + "attributes/", page="attributes")
        attributes = dict((x.capitalize(), y) for (x, y) in response.context["object_attributes"])
        self.assertEqual(attributes["E-mail address"], self.user.email)
        self.assertTrue(response.context["is_owner"])

    def test_groups(self):
        response = self.get(self.user_url + "groups/")
        # TODO

    def test_part_doc_cads(self):
        response = self.get(self.user_url + "parts-doc-cad/")
        # TODO

    def test_history(self):
        response = self.get(self.user_url + "history/")

    def test_navigate(self):
        response = self.get(self.user_url + "navigate/")

    def test_sponsor_get(self):
        response = self.get(self.user_url + "delegation/sponsor/")
        form = response.context["sponsor_form"]
        self.assertEquals(
            set(g.id for g in self.user.groupinfo_owner.all()), set(g.id for g in form.fields["groups"].queryset.all())
        )

    def test_sponsor_post(self):
        data = dict(
            sponsor=self.user.id,
            username="******",
            first_name="You",
            last_name="Lost",
            email="*****@*****.**",
            groups=[self.group.pk],
        )
        response = self.post(self.user_url + "delegation/sponsor/", data)
        user = User.objects.get(username=data["username"])
        for attr in ("first_name", "last_name", "email"):
            self.assertEquals(data[attr], getattr(user, attr))
        self.assertTrue(user.get_profile().is_contributor)
        self.assertFalse(user.get_profile().is_administrator)
        self.assertTrue(user.groups.filter(id=self.group.id))

    def test_modify_get(self):
        response = self.get(self.user_url + "modify/")
        form = response.context["modification_form"]
        self.assertEqual(self.user.first_name, form.initial["first_name"])
        self.assertEqual(self.user.email, form.initial["email"])

    def test_modify_post(self):
        data = {"last_name": "Snow", "email": "*****@*****.**", "first_name": "John"}
        response = self.post(self.user_url + "modify/", data)
        user = User.objects.get(username=self.user.username)
        self.assertEqual("Snow", user.last_name)

    def test_password_get(self):
        response = self.get(self.user_url + "password/")
        self.assertTrue(response.context["modification_form"])

    def test_password_post(self):
        data = dict(old_password="******", new_password1="pw", new_password2="pw")
        response = self.post(self.user_url + "password/", data)
        self.user = User.objects.get(pk=self.user.pk)
        self.assertTrue(self.user.check_password("pw"))

    def test_password_error(self):
        data = dict(old_password="******", new_password1="pw", new_password2="pw")
        response = self.post(self.user_url + "password/", data)
        self.user = User.objects.get(pk=self.user.pk)
        self.assertTrue(self.user.check_password("password"))
        self.assertFalse(self.user.check_password("pw"))

    def test_delegation_get(self):
        response = self.get(self.user_url + "delegation/")

    def test_delegation_remove(self):
        self.controller.delegate(self.brian, m.ROLE_OWNER)
        link = self.controller.get_user_delegation_links()[0]
        data = {"link_id": link.id}
        response = self.post(self.user_url + "delegation/", data)
        self.assertFalse(self.controller.get_user_delegation_links())

    def test_delegate_get(self):
        for role in ("owner", "notified"):
            url = self.user_url + "delegation/delegate/%s/" % role
            response = self.get(url, link=True, page="delegation")
            self.assertEqual(role, unicode(response.context["role"]))

    def test_delegate_sign_get(self):
        for level in ("all", "1", "2"):
            url = self.user_url + "delegation/delegate/sign/%s/" % str(level)
            response = self.get(url, link=True, page="delegation")
            role = unicode(response.context["role"])
            self.assertTrue(role.startswith("signer"))
            self.assertTrue(level in role)

    def test_delegate_post(self):
        data = {"type": "User", "username": self.brian.username}
        for role in ("owner", "notified"):
            url = self.user_url + "delegation/delegate/%s/" % role
            response = self.post(url, data)
            m.DelegationLink.objects.get(role=role, delegator=self.user, delegatee=self.brian)

    def test_delegate_sign_post(self):
        data = {"type": "User", "username": self.brian.username}
        for level in xrange(1, 4):
            url = self.user_url + "delegation/delegate/sign/%d/" % level
            response = self.post(url, data)
            role = level_to_sign_str(level - 1)
            m.DelegationLink.objects.get(role=role, delegator=self.user, delegatee=self.brian)

    def test_delegate_sign_all_post(self):
        # sign all level
        data = {"type": "User", "username": self.brian.username}
        url = self.user_url + "delegation/delegate/sign/all/"
        response = self.post(url, data)
        for level in xrange(2):
            role = level_to_sign_str(level)
            m.DelegationLink.objects.get(role=role, delegator=self.user, delegatee=self.brian)
コード例 #14
0
ファイル: navigate.py プロジェクト: pcon-world/pcon_db
 def setUp(self):
     super(UserNavigateTestCase, self).setUp()
     self.part = self.controller
     self.controller = UserController(self.user, self.user)
コード例 #15
0
ファイル: user.py プロジェクト: amarh/openPLM
 def setUp(self):
     super(UserViewTestCase, self).setUp()
     self.user_url = "/user/%s/" % self.user.username
     self.controller = UserController(self.user, self.user)
コード例 #16
0
class UserViewTestCase(CommonViewTest):
    def setUp(self):
        super(UserViewTestCase, self).setUp()
        self.user_url = "/user/%s/" % self.user.username
        self.controller = UserController(self.user, self.user)

    def test_user_attribute(self):
        response = self.get(self.user_url + "attributes/", page="attributes")
        attributes = dict(
            (x.capitalize(), y)
            for (x, y, z) in response.context["object_attributes"])

        old_lang = translation.get_language()
        translation.activate(self.LANGUAGE)
        key = _("email address")
        translation.activate(old_lang)
        del old_lang

        self.assertEqual(attributes[key.capitalize()], self.user.email)
        self.assertTrue(response.context["is_owner"])

    def test_groups(self):
        response = self.get(self.user_url + "groups/")
        # TODO

    def test_part_doc_cads(self):
        response = self.get(self.user_url + "parts-doc-cad/")
        # TODO

    def test_history(self):
        response = self.get(self.user_url + "history/")

    def test_navigate(self):
        response = self.get(self.user_url + "navigate/")

    def test_sponsor_get(self):
        response = self.get(self.user_url + "delegation/sponsor/")
        form = response.context["sponsor_form"]
        self.assertEquals(
            set(g.id for g in self.user.groupinfo_owner.all()),
            set(g.id for g in form.fields["groups"].queryset.all()))

    def test_sponsor_post(self):
        data = dict(sponsor=self.user.id,
                    username="******",
                    first_name="You",
                    last_name="Lost",
                    email="*****@*****.**",
                    groups=[self.group.pk],
                    language=self.user.profile.language)
        response = self.post(self.user_url + "delegation/sponsor/", data)
        user = User.objects.get(username=data["username"])
        for attr in ("first_name", "last_name", "email"):
            self.assertEquals(data[attr], getattr(user, attr))
        self.assertTrue(user.profile.is_contributor)
        self.assertFalse(user.profile.is_administrator)
        self.assertTrue(user.groups.filter(id=self.group.id))

    def test_modify_get(self):
        response = self.get(self.user_url + "modify/")
        form = response.context["modification_form"]
        self.assertEqual(self.user.first_name, form.initial["first_name"])
        self.assertEqual(self.user.email, form.initial["email"])

    def test_modify_post(self):
        data = {
            "last_name": "Snow",
            "email": "*****@*****.**",
            "first_name": "John",
            "avatar": "",
        }
        response = self.post(self.user_url + "modify/", data)
        user = User.objects.get(username=self.user.username)
        self.assertEqual("Snow", user.last_name)

    def test_modify_sponsored_user(self):
        data0 = dict(sponsor=self.user.id,
                     username="******",
                     first_name="You",
                     last_name="Lost",
                     email="*****@*****.**",
                     groups=[self.group.pk],
                     language=self.user.profile.language)
        response = self.post(self.user_url + "delegation/sponsor/", data0)
        data = {
            "last_name": "Snow",
            "email": "*****@*****.**",
            "first_name": "John",
            "avatar": None,
        }
        # brian can not edit these data
        self.client.login(username=self.brian.username, password="******")
        response = self.client.post("/user/loser/modify/", data)
        user = User.objects.get(username="******")
        self.assertEqual(user.email, data0["email"])
        self.assertEqual(user.first_name, data0["first_name"])
        self.assertEqual(user.last_name, data0["last_name"])

        # self.user can edit these data
        self.client.login(username=self.user.username, password="******")
        response = self.client.post("/user/loser/modify/", data)
        user = User.objects.get(username="******")
        self.assertEqual(user.email, data["email"])
        self.assertEqual(user.first_name, data["first_name"])
        self.assertEqual(user.last_name, data["last_name"])

        # it should not be possible to edit data once loser has logged in
        user.set_password("pwd")
        user.save()
        self.client.login(username=user.username, password="******")
        self.client.get("/home/")
        self.client.login(username=self.user.username, password="******")
        data2 = {
            "last_name": "Snow2",
            "email": "*****@*****.**",
            "first_name": "John2"
        }
        response = self.client.post("/user/loser/modify/", data2)
        user = User.objects.get(username="******")
        self.assertEqual(user.email, data["email"])
        self.assertEqual(user.first_name, data["first_name"])
        self.assertEqual(user.last_name, data["last_name"])

    def test_password_get(self):
        response = self.get(self.user_url + "password/")
        self.assertTrue(response.context["modification_form"])

    def test_password_post(self):
        data = dict(old_password="******",
                    new_password1="pw",
                    new_password2="pw")
        response = self.post(self.user_url + "password/", data)
        self.user = User.objects.get(pk=self.user.pk)
        self.assertTrue(self.user.check_password("pw"))

    def test_password_error(self):
        data = dict(old_password="******",
                    new_password1="pw",
                    new_password2="pw")
        response = self.post(self.user_url + "password/", data)
        self.user = User.objects.get(pk=self.user.pk)
        self.assertTrue(self.user.check_password("password"))
        self.assertFalse(self.user.check_password("pw"))

    def test_delegation_get(self):
        response = self.get(self.user_url + "delegation/")

    def test_delegation_remove(self):
        self.controller.delegate(self.brian, m.ROLE_OWNER)
        link = self.controller.get_user_delegation_links()[0]
        data = {"link_id": link.id}
        response = self.post(self.user_url + "delegation/delete/", data)
        self.assertFalse(self.controller.get_user_delegation_links())

    def test_delegate_get(self):
        for role in ("owner", "notified"):
            url = self.user_url + "delegation/delegate/%s/" % role
            response = self.get(url, link=True, page="delegation")
            self.assertEqual(role, unicode(response.context["role"]))

    def test_delegate_sign_get(self):
        for level in ("all", "1", "2"):
            url = self.user_url + "delegation/delegate/sign/%s/" % str(level)
            response = self.get(url, link=True, page="delegation")
            if self.LANGUAGE == "en":
                role = unicode(response.context["role"])
                self.assertTrue(role.startswith("signer"))
                self.assertTrue(level in role)

    def test_delegate_post(self):
        data = {"type": "User", "username": self.brian.username}
        for role in ("owner", "notified"):
            url = self.user_url + "delegation/delegate/%s/" % role
            response = self.post(url, data)
            m.DelegationLink.objects.get(role=role,
                                         delegator=self.user,
                                         delegatee=self.brian)

    def test_delegate_sign_post(self):
        data = {"type": "User", "username": self.brian.username}
        for level in xrange(1, 4):
            url = self.user_url + "delegation/delegate/sign/%d/" % level
            response = self.post(url, data)
            role = level_to_sign_str(level - 1)
            m.DelegationLink.objects.get(role=role,
                                         delegator=self.user,
                                         delegatee=self.brian)

    def test_delegate_sign_all_post(self):
        # sign all level
        data = {"type": "User", "username": self.brian.username}
        url = self.user_url + "delegation/delegate/sign/all/"
        response = self.post(url, data)
        for level in xrange(2):
            role = level_to_sign_str(level)
            m.DelegationLink.objects.get(role=role,
                                         delegator=self.user,
                                         delegatee=self.brian)

    def test_resend_sponsor_mail(self):
        user = User(username="******", email="*****@*****.**")
        self.controller.sponsor(user)
        link = m.DelegationLink.objects.get(role="sponsor", delegatee=user)
        pwd = user.password
        mail.outbox = []
        self.post(self.user_url + 'delegation/sponsor/mail/',
                  {"link_id": link.id})
        self.assertEqual(1, len(mail.outbox))
        self.assertEqual(mail.outbox[0].bcc, [user.email])
        user = User.objects.get(username="******")
        self.assertNotEqual(user.password, pwd)

    def test_resend_sponsor_error_user_connected(self):
        user = User(username="******", email="*****@*****.**")
        self.controller.sponsor(user)
        user.last_login = timezone.now()
        user.save()
        link = m.DelegationLink.objects.get(role="sponsor", delegatee=user)
        pwd = user.password
        mail.outbox = []
        self.post(self.user_url + 'delegation/sponsor/mail/',
                  {"link_id": link.id},
                  status_code=403)
        self.assertEqual(0, len(mail.outbox))
        user = User.objects.get(username="******")
        self.assertEqual(user.password, pwd)

    def test_resend_sponsor_error_not_sponsor(self):
        user = User(username="******", email="*****@*****.**")
        UserController(self.cie, self.cie).sponsor(user)
        link = m.DelegationLink.objects.get(role="sponsor", delegatee=user)
        pwd = user.password
        mail.outbox = []
        self.post(self.user_url + 'delegation/sponsor/mail/',
                  {"link_id": link.id},
                  status_code=403)
        self.assertEqual(0, len(mail.outbox))
        user = User.objects.get(username="******")
        self.assertEqual(user.password, pwd)

    def test_upload_file_get(self):
        response = self.get(self.user_url + "files/add/")
        self.assertTrue(
            isinstance(response.context["add_file_form"], forms.AddFileForm))

    def test_upload_file_post(self):
        fname = u"toti\xe8o_t.txt"
        name = u"toti\xe8o t"
        f = self.get_file(name=fname, data="crumble")
        data = {"filename": f}
        response = self.post(self.user_url + "files/add/", data)
        df = list(self.controller.files.all())[0]
        self.assertEquals(df.filename, f.name)
        self.assertEquals("crumble", df.file.read())
        url = "/object/create/?type=Document&pfiles=%d" % df.id
        self.assertRedirects(response, url)
        # post the form as previously returned by "files/add/"
        cform = response.context["creation_form"]
        self.assertEquals(name, cform.initial["name"])
        form = lxml.html.fromstring(
            response.content).xpath("//form[@id='creation_form']")[0]
        data = dict(form.fields)
        data["template"] = ""
        r2 = self.post(url, data)
        obj = r2.context["obj"]
        self.assertEquals(name, obj.name)
        self.assertEquals(list(obj.files.values_list("filename", flat=True)),
                          [fname])
        self.assertFalse(self.controller.files.all())
        self.assertEquals(obj.files.all()[0].file.read(), "crumble")
コード例 #17
0
ファイル: tests.py プロジェクト: pcon-world/pcon_db
 def test_calendar_user(self):
     ctrl = UserController(self.user, self.user)
     ctrl.delegate(self.brian, "owner")
     response = self.get("/user/%s/history/calendar/" % self.user.username)
     calendar = response.context["calendar"]
     self.assertTrue("delegation" in calendar)