def test_role_user_state3_perms(self): """ Testing user permission in state3. """ api_key = self.__login("user", "pass3") headers = self.__build_headers("user", api_key) test_author = Author.objects.create(id=100, name="dumb_name") set_state(test_author, self.state3) response = self.client.post("/admin-api/author/", data=self.new_author, content_type='application/json', **headers) tools.assert_equals(response.status_code, 201) response = self.client.get("/admin-api/author/100/", **headers) tools.assert_equals(response.status_code, 200) response = self.client.put("/admin-api/author/100/", data=self.new_author, content_type='application/json', **headers) tools.assert_equals(response.status_code, 401) response = self.client.patch("/admin-api/author/100/", data=self.new_author, content_type='application/json', **headers) tools.assert_equals(response.status_code, 401) response = self.client.delete("/admin-api/author/100/", **headers) tools.assert_equals(response.status_code, 401) self.__logout(headers)
def test_set_state_of_two_models(self): """ This shoudn't failed with `MultipleObjectsReturned`. """ api_key = self.__login("user", "pass") headers = self.__build_headers("user", api_key) self.workflow.set_to_model(Author) set_state(self.author, self.state1) set_state(self.author_two, self.state1) author_patch = json.dumps({ "state": self.state2.codename }) response = self.client.patch("/admin-api/author/100/", data=author_patch, content_type='application/json', **headers) tools.assert_equals(response.status_code, 202) tools.assert_equals(get_state(self.author), self.state2) response = self.client.patch("/admin-api/author/101/", data=author_patch, content_type='application/json', **headers) tools.assert_equals(response.status_code, 202) tools.assert_equals(get_state(self.author_two), self.state2) response = self.client.get("/admin-api/author/", **headers) tools.assert_equals(response.status_code, 200) self.__logout(headers)
def save(self, bundle, skip_errors=False): bundle = super(ApiModelResource, self).save(bundle, skip_errors=skip_errors) # we need save state only when the object is created # so it is not possible to call this in hydrate or full_hydrate # because it will not work for new objects if "state" in bundle.data: set_state(bundle.obj, bundle.data["state"]) return bundle
def _add_state_fields(self, bundle): """Adds current state and next allowed states of object.""" state = get_state(bundle.obj) if (((state and state.codename != "published") or not state) and bundle.obj and bundle.obj.published): set_state(bundle.obj, "published") state = get_state(bundle.obj) if state: bundle.data["state"] = state.codename # FIXME: Use correct transition table bundle.data["allowed_states"] = State.objects.get_states_choices_as_dict() return bundle
def test_set_state_via_api(self): api_key = self.__login("user", "pass") headers = self.__build_headers("user", api_key) self.workflow.set_to_model(Author) set_state(self.author, self.state1) author_patch = json.dumps({ "state": self.state2.codename }) response = self.client.patch("/admin-api/author/100/", data=author_patch, content_type='application/json', **headers) tools.assert_equals(get_state(self.author), self.state2) self.__logout(headers)
def test_filter_by_state(self): """ State is virtual field so filtering has to be done manually. """ author1 = Author.objects.create(id=self.user.id, name="1st author", slug="one") author2 = Author.objects.create(name="2nd author", slug="two") api_key = self.__login("user", "pass") headers = self.__build_headers("user", api_key) self.workflow.set_to_model(Author) set_state(author1, self.state1) set_state(author2, self.state2) self.workflow.set_to_model(User) set_state(self.user, self.state2) url = u"/admin-api/author/?state=" + self.state2.codename response = self.client.get(url, **headers) tools.assert_equals(response.status_code, 200, response.content) resources = self.__get_response_json(response) tools.assert_equals(len(resources), 1) tools.assert_equals(resources[0]["state"], self.state2.codename) self.__logout(headers)
def test_state_included(self): api_key = self.__login("user", "pass") headers = self.__build_headers("user", api_key) self.workflow.set_to_model(Author) set_state(self.author, self.state1) author_patch = json.dumps({ "state": self.state2.codename }) response = self.client.patch("/admin-api/author/100/", data=author_patch, content_type='application/json', **headers) tools.assert_equals(response.status_code, 202) response = self.client.get("/admin-api/author/100/", **headers) tools.assert_equals(response.status_code, 200) resource = self.__get_response_json(response) tools.assert_in("state", resource) tools.assert_equals(resource["state"], self.state2.codename) tools.assert_in("allowed_states", resource) tools.assert_in(self.state3.codename, resource["allowed_states"]) self.__logout(headers)
def test_set_state(self): set_state(self.author, self.state1) set_state(self.author, self.state2) tools.assert_equals(get_state(self.author), self.state2)