def set_up_users(self): superuser = User.objects.create_user("superuser", "*****@*****.**", "superuser") superuser.is_superuser = True superuser.is_staff = True superuser.save() self.login_as("superuser") new_user_target = WebTarget("POST", user_management.views.new_user) expectation = ViewExpectation(Conditions.null(), new_user_target) for name in ("alice", "bob", "carol", "dave"): ## Test creation of the account. input_context = { "username": name, "first_name": name.capitalize(), "last_name": "Example", "email": "*****@*****.**" % name, } expectation.check(self, input_context) ## Park the corresponding User object as an attribute, and ## reset its (randomly-generated) password, ## so that other test code can simulate logins over the client. u = User.objects.get(username=name) self.__dict__[name] = u u.set_password(name) u.save() g = Group.objects.create(name="taggers_and_mergers") for member in (self.bob, self.carol, self.dave): g.user_set.add(member) g.save() self.taggers_and_mergers = g
def tag_project(self): ## Each user should tag two tasks. for i in range(2): for user in (self.bob, self.carol, self.dave): self.login_as(user.username) next_task_target = WebTarget("GET", main.views.base.next_task, final_views=(main.views.task.task_view,)) next_task_expectation = ViewExpectation(Conditions.null(), next_task_target) output_context = next_task_expectation.check(self) question = output_context["question"] answer_input_context = {"answer": self.quiz[question][user], "comment": "generated in testing"} answer_target = WebTarget("POST", main.views.task.task_view, (output_context["task"]["id"],), final_views=(main.views.task.task_view, main.views.base.home)) answer_expectation = ViewExpectation(Conditions.null(), answer_target) answer_expectation.check(self, answer_input_context) print >>sys.stderr, "User", user.username, "answered", question ## At this point the user is holding a WIP for the third task, ## but we want that WIP to be released. abandon_target = WebTarget("POST", main.views.base.abandon_wip, statuses=(302, 200)) abandon_expectation = ViewExpectation(Conditions.null(), abandon_target) abandon_expectation.check(self)
def get_next_task_view(self): self.client.login(username="******", password="******") ## get the next task self.task_expectation.check(self) ## tweak the database to put a response and a review in response = Response(task=self.t, user=self.user, start_time=timezone.now()) response.full_clean() response.save() review = Review(response=response, comment="I have reviewed this task.") review.full_clean() review.save() ## check to see if next_task now redirects to review ## if we actually execute the view code, we will get an error, ## because the task associated with the review has no result; ## therefore the code below is a bit hackish review_target = WebTarget("GET", main.views.base.next_task, statuses=(301, 302)) def redirects_to_review(input_context, output_context): return "/review/next/" in output_context["__redirected_to__"] review_expectation = ViewExpectation( Conditions.post(redirects_to_review), review_target) review_expectation.check(self) ## tweak the database to complete the review review.complete = True review.full_clean() review.save() ## try getting the next task again self.task_expectation.check(self)
def set_up_users(self): superuser = User.objects.create_user("superuser", "*****@*****.**", "superuser") superuser.is_superuser = True superuser.is_staff = True superuser.save() self.login_as("superuser") new_user_target = WebTarget("POST", user_management.views.new_user) expectation = ViewExpectation(Conditions.null(), new_user_target) for name in ("alice", "bob", "carol", "dave"): ## Test creation of the account. input_context = {"username": name, "first_name": name.capitalize(), "last_name": "Example", "email": "*****@*****.**" % name} expectation.check(self, input_context) ## Park the corresponding User object as an attribute, and ## reset its (randomly-generated) password, ## so that other test code can simulate logins over the client. u = User.objects.get(username=name) self.__dict__[name] = u u.set_password(name) u.save() g = Group.objects.create(name="taggers_and_mergers") for member in (self.bob, self.carol, self.dave): g.user_set.add(member) g.save() self.taggers_and_mergers = g
def export_project_dict(self): self.client.login(username="******", password="******") target = WebTarget("GET", main.views.project.project_export, args=(self.p.id, )) expectation = ViewExpectation(Conditions.null(), target) expectation.check(self)
def get_next_task_view(self): self.client.login(username="******", password="******") ## get the next task self.task_expectation.check(self) ## tweak the database to put a response and a review in response = Response(task=self.t, user=self.user, start_time=datetime.datetime.now()) response.full_clean() response.save() review = Review(response=response, comment="I have reviewed this task.") review.full_clean() review.save() ## check to see if next_task now redirects to review ## if we actually execute the view code, we will get an error, ## because the task associated with the review has no result; ## therefore the code below is a bit hackish review_target = WebTarget("GET", main.views.base.next_task, statuses=(301, 302)) def redirects_to_review(input_context, output_context): return "/review/next/" in output_context["__redirected_to__"] review_expectation = ViewExpectation(Conditions.post(redirects_to_review), review_target) review_expectation.check(self) ## tweak the database to complete the review review.complete = True review.full_clean() review.save() ## try getting the next task again self.task_expectation.check(self)
def test_home_post(self): """ You can't post to /; it's @get only. """ self.test_login() target = WebTarget("POST", main.views.base.home, statuses=(405, )) expectation = ViewExpectation(Conditions.null(), target) expectation.check(self)
def test_home_post(self): """ You can't post to /; it's @get only. """ self.test_login() target = WebTarget("POST", main.views.base.home, statuses=(405,)) expectation = ViewExpectation(Conditions.null(), target) expectation.check(self)
def populate_project(self): f = SimpleUploadedFile("questions", "\n".join(self.quiz.keys())) f.open("rb") context = {"action": "Upload", "upload": f} target = WebTarget("POST", main.views.project.project_upload, args=(self.project.id,)) expectation = ViewExpectation(Conditions.null(), target) expectation.check(self, context)
def populate_project(self): f = SimpleUploadedFile("questions", "\n".join(list(self.quiz.keys()))) f.open("rb") context = {"action": "Upload", "upload": f} target = WebTarget("POST", main.views.project.project_upload, args=(self.project.id, )) expectation = ViewExpectation(Conditions.null(), target) expectation.check(self, context)
def get_next_task_should_fail(self): """Try to get a task with the wrong user's authentication.""" other_user = User.objects.create_user("nobody_special", "*****@*****.**", "abc") ## this should work... self.client.login(username="******", password="******") self.task_expectation.check(self) ## ...and this shouldn't self.client.login(username="******", password="******") forbidden_task_target = WebTarget("GET", main.views.task.task_view, args=(self.t.id,), statuses=(403,)) forbidden_expectation = ViewExpectation(Conditions.null(), forbidden_task_target) forbidden_expectation.check(self)
def get_next_task_should_fail(self): """Try to get a task with the wrong user's authentication.""" other_user = User.objects.create_user("nobody_special", "*****@*****.**", "abc") ## this should work... self.client.login(username="******", password="******") self.task_expectation.check(self) ## ...and this shouldn't self.client.login(username="******", password="******") forbidden_task_target = WebTarget("GET", main.views.task.task_view, args=(self.t.id, ), statuses=(403, )) forbidden_expectation = ViewExpectation(Conditions.null(), forbidden_task_target) forbidden_expectation.check(self)
def tag_project(self): ## Each user should tag two tasks. for i in range(2): for user in (self.bob, self.carol, self.dave): self.login_as(user.username) next_task_target = WebTarget( "GET", main.views.base.next_task, final_views=(main.views.task.task_view, ), ) next_task_expectation = ViewExpectation( Conditions.null(), next_task_target) output_context = next_task_expectation.check(self) question = output_context["question"] answer_input_context = { "answer": self.quiz[question][user], "comment": "generated in testing", } answer_target = WebTarget( "POST", main.views.task.task_view, (output_context["task"]["id"], ), final_views=(main.views.task.task_view, main.views.base.home), ) answer_expectation = ViewExpectation(Conditions.null(), answer_target) answer_expectation.check(self, answer_input_context) print("User", user.username, "answered", question, file=sys.stderr) ## At this point the user is holding a WIP for the third task, ## but we want that WIP to be released. abandon_target = WebTarget("POST", main.views.base.abandon_wip, statuses=(302, 200)) abandon_expectation = ViewExpectation(Conditions.null(), abandon_target) abandon_expectation.check(self)
def test_home(self): self.client.login(username="******", password="******") target = WebTarget("GET", main.views.base.home) def honey_i_am_home(input_context, output_context): """Check for JSON output that could only have come from the home page.""" return all([ "respondable_tasks" in output_context, "respondable_task_count" in output_context, "resolvable_tasks" in output_context, "resolvable_task_count" in output_context, "recent_responses" in output_context, "reviews" in output_context, ]) def home_empty_counts(input_context, output_context): return (output_context["respondable_task_count"] == 0 and output_context["resolvable_task_count"] == 0) expectation = ViewExpectation( Conditions((), (honey_i_am_home, home_empty_counts), ()), target) expectation.check(self)
def test_home(self): self.client.login(username="******", password="******") target = WebTarget("GET", main.views.base.home) def honey_i_am_home(input_context, output_context): """Check for JSON output that could only have come from the home page.""" return all( [ "respondable_tasks" in output_context, "respondable_task_count" in output_context, "resolvable_tasks" in output_context, "resolvable_task_count" in output_context, "recent_responses" in output_context, "reviews" in output_context, ] ) def home_empty_counts(input_context, output_context): return output_context["respondable_task_count"] == 0 and output_context["resolvable_task_count"] == 0 expectation = ViewExpectation(Conditions((), (honey_i_am_home, home_empty_counts), ()), target) expectation.check(self)
def export_project_simple(self): def export_attrs(input_context, output_context): zipped_64 = output_context["contents_in_base64"] zipped = ZipFile(StringIO(b64decode(zipped_64)), "r", ZIP_DEFLATED) permissions_are_right = [ zi.external_attr == settings.CLICKWORK_EXPORT_FILE_PERMISSIONS << 16 for zi in zipped.infolist() ] return all(permissions_are_right) self.client.login(username="******", password="******") target = WebTarget("GET", main.views.project.project_export, args=(self.p.id, )) expectation = ViewExpectation(Conditions.post(export_attrs), target) output_context = expectation.check(self)
def export_project_dict(self): self.client.login(username="******", password="******") target = WebTarget("GET", main.views.project.project_export, args=(self.p.id,)) expectation = ViewExpectation(Conditions.null(), target) expectation.check(self)
self.user = u self.p = p def export_project_simple(self): def export_attrs(input_context, output_context): zipped_64 = output_context["contents_in_base64"] zipped = ZipFile(StringIO(b64decode(zipped_64)), "r", ZIP_DEFLATED) permissions_are_right = [ zi.external_attr == settings.CLICKWORK_EXPORT_FILE_PERMISSIONS << 16L for zi in zipped.infolist() ] return all(permissions_are_right) self.client.login(username="******", password="******") target = WebTarget("GET", main.views.project.project_export, args=(self.p.id,)) expectation = ViewExpectation(Conditions.post(export_attrs), target) output_context = expectation.check(self) def export_project_dict(self): self.client.login(username="******", password="******") target = WebTarget("GET", main.views.project.project_export, args=(self.p.id,)) expectation = ViewExpectation(Conditions.null(), target) expectation.check(self) class GetNextTask(TestCase): def setUp(self): u = User.objects.create_user("testuser_getnexttask", "*****@*****.**", "abc") u.full_clean() u.save() g = Group(name="test group") g.full_clean()