def test_changing_access_from_restricted_to_private_clears_grantees(self):
        TemplateGrantee(grantee=self.char2.roster,
                        template=self.c2_template).save()

        self.assertEqual(self.c2_template.grantees.count(), 1)

        self.setup_cmd(CmdTemplateForm, self.char1)
        self.call_cmd(
            "/change_access {}={}".format(self.c2_template.id, "PRIVATE"),
            "You have changed the access level to PRIVATE.",
        )

        self.assertEqual(self.c2_template.grantees.count(), 0)

        self.setup_cmd(CmdTemplateForm, self.char1)
        self.call_cmd(
            "/change_access {}={}".format(self.c2_template.id, "RESTRICTED"),
            "You have changed the access level to RESTRICTED.",
        )

        TemplateGrantee(grantee=self.char2.roster,
                        template=self.c2_template).save()

        self.assertEqual(self.c2_template.grantees.count(), 1)

        self.setup_cmd(CmdTemplateForm, self.char1)
        self.call_cmd(
            "/change_access {}={}".format(self.c2_template.id, "OPEN"),
            "You have changed the access level to OPEN.",
        )

        self.assertEqual(self.c2_template.grantees.count(), 0)
    def test_can_get_list_of_grantees(self):
        self.setup_cmd(CmdTemplateForm, self.char1)
        self.call_cmd(
            "/grantees {}".format(self.c1_template.id),
            "No one but you has access to this template. It has a PRIVATE access level.",
        )

        self.c1_template.access_level = "OP"
        self.c1_template.save()

        self.call_cmd(
            "/grantees {}".format(self.c1_template.id),
            "Everyone has access to this template. It has an OPEN access level.",
        )

        self.c1_template.access_level = "RS"
        self.c1_template.save()

        TemplateGrantee(grantee=self.char2.roster,
                        template=self.c1_template).save()

        self.call_cmd(
            "/grantees {}".format(self.c1_template.id),
            "Current grantees: {}".format(self.char2.name),
        )

        self.call_cmd(
            "/grantees",
            "Which template do you want to get the list of grantees for?")
    def test_can_revoke_access_to_a_template(self):
        TemplateGrantee(grantee=self.char2.roster,
                        template=self.c2_template).save()

        self.setup_cmd(CmdTemplateForm, self.char1)
        self.call_cmd(
            "/revoke {}={}".format(self.char2.name, self.c2_template.id),
            "You have revoked {}'s access to [[TEMPLATE_{}]].".format(
                self.char2.name, self.c2_template.id),
        )

        self.call_cmd(
            "/grant {}={}".format(self.char2.name, self.c1_template.id),
            "Template must have access level of RESTRICTED to modify grantees.",
        )

        self.setup_cmd(CmdTemplateForm, self.char1)
        self.call_cmd(
            "/revoke {}={}".format(self.char2.name, self.c2_template.id),
            "{} does not have access to [[TEMPLATE_{}]].".format(
                self.char2.name, self.c2_template.id),
        )

        self.setup_cmd(CmdTemplateForm, self.char2)
        self.call_cmd("/list", self.create_table_view([]))

        self.call_cmd("/revoke Char2=12312312312",
                      "You do not own a template with that id.")
        self.call_cmd("/revoke Char-1={}".format(self.c2_template.id),
                      "Char-1 does not exist.")
    def test_other_player_cannot_see_restricted_templates_where_they_do_not_have_access(self):
        restricted = self.create_template_for(self.paccount1, title="Other Template", access_level="RS")
        restricted.save()

        self.setup_cmd(CmdTemplateForm, self.char2)
        self.call_cmd("/list", self.create_table_view([]))

        TemplateGrantee(grantee=self.char2.roster, template=restricted).save()
        restricted.save()

        self.setup_cmd(CmdTemplateForm, self.char2)
        self.call_cmd("/list", self.create_table_view([restricted]))
    def test_can_describe_template(self):
        self.setup_cmd(CmdTemplateForm, self.char1)
        self.call_cmd("{}".format(self.c2_template.id),
                      "Creator: {}\nDesc: {}".format(self.c2_template.attribution, self.c2_template.desc))

        self.setup_cmd(CmdTemplateForm, self.char2)
        self.call_cmd("{}".format(self.c2_template.id), "You do not have access to a template with that id.")

        TemplateGrantee(grantee=self.char2.roster, template=self.c2_template).save()

        self.call_cmd("{}".format(self.c2_template.id),
                      "Creator: {}\nDesc: {}".format(self.c2_template.attribution, self.c2_template.desc))