def test_unchoose_chosen(self): # type: () -> None u1 = User.objects.get(username='******') u2 = User.objects.get(username='******') qed = Question.objects.get(title="Text Editor") qos = Question.objects.get(title="Operating System") vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") self.assertEqual(choose(u1, vim), True) self.assertEqual(choose(u1, linux), True) self.assertEqual(choose(u2, vim), True) self.assertEqual(choose(u2, linux), True) self.assertEqual(unchoose(u1, vim), True) self.assertEqual(unchoose(u1, linux), True) ed_choices1 = Choice.objects.filter(user=u1, option__question=qed) os_choices1 = Choice.objects.filter(user=u1, option__question=qos) ed_choices2 = Choice.objects.filter(user=u2, option__question=qed) os_choices2 = Choice.objects.filter(user=u2, option__question=qos) self.assertEqual(ed_choices1.count(), 0) self.assertEqual(os_choices1.count(), 0) self.assertEqual(ed_choices2.count(), 1) self.assertEqual(os_choices2.count(), 1) self.assertEqual(ed_choices2[0].option_id, vim.id) self.assertEqual(os_choices2[0].option_id, linux.id) self.assertEqual(vote_count(vim), 1) self.assertEqual(vote_count(linux), 1)
def do_test_login(test, username, password, status_code1, status_code2, content_type=None, output1=None, output2=None, as_dict=True, login_username=None): # type: (TestCase, text_type, text_type, int, int, Optional[text_type], Optional[text_type], Optional[text_type], bool, Optional[text_type]) -> None if login_username is None: login_username = username user = User.objects.get(username=login_username) vim = Option.objects.get(text="Vim") linux = Option.objects.get(text="Linux") choose(user, vim) choose(user, linux) if as_dict: login_data = {"username": username, "password": password} # type: Union[List[text_type], Dict[text_type, text_type]] else: login_data = [username, password] data = encode_data(login_data) login_response = send_request(test.client.post, '/api/login/', data, content_type) test.assertEqual(login_response.status_code, status_code1) if output1 is not None: test.assertEqual(get_response_str(login_response), output1) response = test.client.get('/api/my-choices/') if status_code2 is not None: test.assertEqual(response.status_code, status_code2) if status_code2 == 200: choices = json.loads(get_response_str(response)) test.assertEqual(choices, sorted([vim.id, linux.id])) else: if output2 is not None: test.assertEqual(get_response_str(response), output2)
def test_same_choose(self): # type: () -> None user = User.objects.get(username='******') qed = Question.objects.get(title="Text Editor") qos = Question.objects.get(title="Operating System") vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") self.assertEqual(choose(user, vim), True) self.assertEqual(choose(user, linux), True) ed_choices1 = Choice.objects.filter(user=user, option__question=qed) os_choices1 = Choice.objects.filter(user=user, option__question=qos) self.assertEqual(ed_choices1.count(), 1) self.assertEqual(os_choices1.count(), 1) self.assertEqual(ed_choices1.first().option_id, vim.id) self.assertEqual(os_choices1.first().option_id, linux.id) self.assertEqual(choose(user, vim), False) self.assertEqual(choose(user, linux), False) ed_choices2 = Choice.objects.filter(user=user, option__question=qed) os_choices2 = Choice.objects.filter(user=user, option__question=qos) self.assertEqual(ed_choices2.count(), 1) self.assertEqual(os_choices2.count(), 1) self.assertEqual(ed_choices2.first().option_id, vim.id) self.assertEqual(os_choices2.first().option_id, linux.id)
def test_unchoose_chosen(self): # type: () -> None u1 = User.objects.get(username='******') u2 = User.objects.get(username='******') qed = Question.objects.get(title="Text Editor") qos = Question.objects.get(title="Operating System") vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") self.assertEqual(choose(u1, vim), True) self.assertEqual(choose(u1, linux), True) self.assertEqual(choose(u2, vim), True) self.assertEqual(choose(u2, linux), True) self.assertEqual(unchoose(u1, vim), True) self.assertEqual(unchoose(u1, linux), True) ed_choices1 = Choice.objects.filter(user=u1, option__question=qed) os_choices1 = Choice.objects.filter(user=u1, option__question=qos) ed_choices2 = Choice.objects.filter(user=u2, option__question=qed) os_choices2 = Choice.objects.filter(user=u2, option__question=qos) self.assertEqual(ed_choices1.count(), 0) self.assertEqual(os_choices1.count(), 0) self.assertEqual(ed_choices2.count(), 1) self.assertEqual(os_choices2.count(), 1) self.assertEqual(ed_choices2[0].option_id, vim.id) self.assertEqual(os_choices2[0].option_id, linux.id) self.assertEqual(vote_count(vim), 1) self.assertEqual(vote_count(linux), 1)
def test_separate_choose(self): # type: () -> None user = User.objects.get(username='******') qed = Question.objects.get(title="Text Editor") qos = Question.objects.get(title="Operating System") vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") atom = Option.objects.get(question=qed, text="Atom") windows = Option.objects.get(question=qos, text="Windows") self.assertEqual(choose(user, atom), True) self.assertEqual(choose(user, windows), True) ed_choices1 = Choice.objects.filter(user=user, option__question=qed) os_choices1 = Choice.objects.filter(user=user, option__question=qos) self.assertEqual(ed_choices1.count(), 1) self.assertEqual(os_choices1.count(), 1) self.assertEqual(ed_choices1[0].option_id, atom.id) self.assertEqual(os_choices1[0].option_id, windows.id) self.assertEqual(choose(user, vim), True) self.assertEqual(choose(user, linux), True) ed_choices2 = Choice.objects.filter( user=user, option__question=qed).order_by('id') os_choices2 = Choice.objects.filter( user=user, option__question=qos).order_by('id') self.assertEqual(ed_choices2.count(), 2) self.assertEqual(os_choices2.count(), 1) self.assertEqual(ed_choices2[0].option_id, atom.id) self.assertEqual(ed_choices2[1].option_id, vim.id) self.assertEqual(os_choices2[0].option_id, linux.id)
def test_unchoose_other(self): # type: () -> None u1 = User.objects.get(username='******') u2 = User.objects.get(username='******') qed = Question.objects.get(title="Text Editor") qos = Question.objects.get(title="Operating System") vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") atom = Option.objects.get(question=qed, text="Atom") windows = Option.objects.get(question=qos, text="Windows") self.assertEqual(choose(u1, vim), True) self.assertEqual(choose(u1, linux), True) self.assertEqual(choose(u2, vim), True) self.assertEqual(choose(u2, atom), True) self.assertEqual(choose(u2, linux), True) self.assertEqual(unchoose(u1, atom), False) self.assertEqual(unchoose(u1, windows), False) ed_choices1 = Choice.objects.filter(user=u1, option__question=qed) os_choices1 = Choice.objects.filter(user=u1, option__question=qos) ed_choices2 = Choice.objects.filter(user=u2, option__question=qed).order_by('id') os_choices2 = Choice.objects.filter(user=u2, option__question=qos).order_by('id') self.assertEqual(ed_choices1.count(), 1) self.assertEqual(os_choices1.count(), 1) self.assertEqual(ed_choices2.count(), 2) self.assertEqual(os_choices2.count(), 1) self.assertEqual(ed_choices1[0].option_id, vim.id) self.assertEqual(os_choices1[0].option_id, linux.id) self.assertEqual(ed_choices2[0].option_id, vim.id) self.assertEqual(ed_choices2[1].option_id, atom.id) self.assertEqual(os_choices2[0].option_id, linux.id) self.assertEqual(vote_count(vim), 2) self.assertEqual(vote_count(linux), 2) self.assertEqual(vote_count(atom), 1)
def test_same_choose(self): # type: () -> None user = User.objects.get(username='******') qed = Question.objects.get(title="Text Editor") qos = Question.objects.get(title="Operating System") vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") self.assertEqual(choose(user, vim), True) self.assertEqual(choose(user, linux), True) ed_choices1 = Choice.objects.filter(user=user, option__question=qed) os_choices1 = Choice.objects.filter(user=user, option__question=qos) self.assertEqual(ed_choices1.count(), 1) self.assertEqual(os_choices1.count(), 1) self.assertEqual(ed_choices1.first().option_id, vim.id) self.assertEqual(os_choices1.first().option_id, linux.id) self.assertEqual(choose(user, vim), False) self.assertEqual(choose(user, linux), False) ed_choices2 = Choice.objects.filter(user=user, option__question=qed) os_choices2 = Choice.objects.filter(user=user, option__question=qos) self.assertEqual(ed_choices2.count(), 1) self.assertEqual(os_choices2.count(), 1) self.assertEqual(ed_choices2.first().option_id, vim.id) self.assertEqual(os_choices2.first().option_id, linux.id)
def test_my_choices_one(self): # type: () -> None user = User.objects.get(username='******') vim = Option.objects.get(text="Vim") choose(user, vim) self.client.force_login(user) response = self.client.get('/api/my-choices/') self.assertEqual(response.status_code, 200) choices = json.loads(get_response_str(response)) self.assertEqual(choices, [vim.id])
def test_my_choices_one(self): # type: () -> None user = User.objects.get(username='******') vim = Option.objects.get(text="Vim") choose(user, vim) self.client.force_login(user) response = self.client.get('/api/my-choices/') self.assertEqual(response.status_code, 200) choices = json.loads(get_response_str(response)) self.assertEqual(choices, [vim.id])
def do_test_vote(test, username, already_chosen_strs, choose_strs, unchoose_strs, status_code, should_be_chosen_strs=None, content_type=None, as_num=False, locked_titles=None): # type: (TestCase, text_type, Optional[Iterable[text_type]], Optional[Iterable[text_type]], Optional[Iterable[text_type]], int, Optional[Iterable[text_type]], Optional[text_type], bool, Optional[Sequence[text_type]]) -> None # Login user user = User.objects.get(username=username) test.client.force_login(user) # choose already_chosen_strs for ostr in already_chosen_strs or []: option = Option.objects.get(text=ostr) choose(user, option) # lock questions in locked_titles Question.objects.filter(title__in=(locked_titles or [])).update( locked=True) # get oid lists choose_list = get_oids_from_strs(choose_strs) unchoose_list = get_oids_from_strs(unchoose_strs) should_be_chosen = get_oids_from_strs(should_be_chosen_strs) if as_num: data = cast(List[int], choose_list or []) + [ -x for x in (unchoose_list or []) ] # type: Union[List[int], Dict[text_type, List[OptionId]]] else: data = {} if choose_list is not None: data["choose"] = choose_list if unchoose_list is not None: data["unchoose"] = unchoose_list response = send_request(test.client.post, '/api/vote/', data, content_type) test.assertEqual(response.status_code, status_code) Question.objects.filter(title__in=(locked_titles or [])).update( locked=False) if should_be_chosen is not None: chosen_by_view = set( Choice.objects.filter(user=user).values_list('option_id', flat=True)) test.assertEqual(chosen_by_view, set(should_be_chosen))
def test_simple_choose(self): # type: () -> None u1 = User.objects.get(username='******') u2 = User.objects.get(username='******') qed = Question.objects.get(title="Text Editor") qos = Question.objects.get(title="Operating System") vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") atom = Option.objects.get(question=qed, text="Atom") choose(u1, vim) choose(u1, linux) choose(u2, atom) choose(u2, linux) ed_choices1 = Choice.objects.filter(user=u1, option__question=qed) ed_choices2 = Choice.objects.filter(user=u2, option__question=qed) os_choices1 = Choice.objects.filter(user=u1, option__question=qos) os_choices2 = Choice.objects.filter(user=u2, option__question=qos) self.assertEqual(ed_choices1.count(), 1) self.assertEqual(ed_choices2.count(), 1) self.assertEqual(os_choices1.count(), 1) self.assertEqual(os_choices2.count(), 1) self.assertEqual(ed_choices1.first().option_id, vim.id) self.assertEqual(ed_choices2.first().option_id, atom.id) self.assertEqual(os_choices1.first().option_id, linux.id) self.assertEqual(os_choices2.first().option_id, linux.id) self.assertEqual(vote_count(vim), 1) self.assertEqual(vote_count(atom), 1) self.assertEqual(vote_count(linux), 2)
def test_simple_choose(self): # type: () -> None u1 = User.objects.get(username='******') u2 = User.objects.get(username='******') qed = Question.objects.get(title="Text Editor") qos = Question.objects.get(title="Operating System") vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") atom = Option.objects.get(question=qed, text="Atom") choose(u1, vim) choose(u1, linux) choose(u2, atom) choose(u2, linux) ed_choices1 = Choice.objects.filter(user=u1, option__question=qed) ed_choices2 = Choice.objects.filter(user=u2, option__question=qed) os_choices1 = Choice.objects.filter(user=u1, option__question=qos) os_choices2 = Choice.objects.filter(user=u2, option__question=qos) self.assertEqual(ed_choices1.count(), 1) self.assertEqual(ed_choices2.count(), 1) self.assertEqual(os_choices1.count(), 1) self.assertEqual(os_choices2.count(), 1) self.assertEqual(ed_choices1.first().option_id, vim.id) self.assertEqual(ed_choices2.first().option_id, atom.id) self.assertEqual(os_choices1.first().option_id, linux.id) self.assertEqual(os_choices2.first().option_id, linux.id) self.assertEqual(vote_count(vim), 1) self.assertEqual(vote_count(atom), 1) self.assertEqual(vote_count(linux), 2)
def do_test_login(test, username, password, status_code1, status_code2, content_type=None, output1=None, output2=None, as_dict=True, login_username=None): # type: (TestCase, text_type, text_type, int, int, Optional[text_type], Optional[text_type], Optional[text_type], bool, Optional[text_type]) -> None if login_username is None: login_username = username user = User.objects.get(username=login_username) vim = Option.objects.get(text="Vim") linux = Option.objects.get(text="Linux") choose(user, vim) choose(user, linux) if as_dict: login_data = { "username": username, "password": password } # type: Union[List[text_type], Dict[text_type, text_type]] else: login_data = [username, password] data = encode_data(login_data) login_response = send_request(test.client.post, '/api/login/', data, content_type) test.assertEqual(login_response.status_code, status_code1) if output1 is not None: test.assertEqual(get_response_str(login_response), output1) response = test.client.get('/api/my-choices/') if status_code2 is not None: test.assertEqual(response.status_code, status_code2) if status_code2 == 200: choices = json.loads(get_response_str(response)) test.assertEqual(choices, sorted([vim.id, linux.id])) else: if output2 is not None: test.assertEqual(get_response_str(response), output2)
def do_test_vote(test, username, already_chosen_strs, choose_strs, unchoose_strs, status_code, should_be_chosen_strs=None, content_type=None, as_num=False, locked_titles=None): # type: (TestCase, text_type, Optional[Iterable[text_type]], Optional[Iterable[text_type]], Optional[Iterable[text_type]], int, Optional[Iterable[text_type]], Optional[text_type], bool, Optional[Sequence[text_type]]) -> None # Login user user = User.objects.get(username=username) test.client.force_login(user) # choose already_chosen_strs for ostr in already_chosen_strs or []: option = Option.objects.get(text=ostr) choose(user, option) # lock questions in locked_titles Question.objects.filter(title__in=(locked_titles or [])).update(locked=True) # get oid lists choose_list = get_oids_from_strs(choose_strs) unchoose_list = get_oids_from_strs(unchoose_strs) should_be_chosen = get_oids_from_strs(should_be_chosen_strs) if as_num: data = cast(List[int], choose_list or []) + [-x for x in (unchoose_list or [])] # type: Union[List[int], Dict[text_type, List[OptionId]]] else: data = {} if choose_list is not None: data["choose"] = choose_list if unchoose_list is not None: data["unchoose"] = unchoose_list response = send_request(test.client.post, '/api/vote/', data, content_type) test.assertEqual(response.status_code, status_code) Question.objects.filter(title__in=(locked_titles or [])).update(locked=False) if should_be_chosen is not None: chosen_by_view = set(Choice.objects.filter(user=user).values_list('option_id', flat=True)) test.assertEqual(chosen_by_view, set(should_be_chosen))
def test_locked(self): # type: () -> None user = User.objects.get(username='******') qed = Question.objects.get(title="Text Editor") qos = Question.objects.get(title="Operating System") vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") atom = Option.objects.get(question=qed, text="Atom") self.assertEqual(choose(user, vim), True) self.assertEqual(choose(user, atom), True) self.assertEqual(choose(user, linux), True) qed.locked = True qed.save() qos.locked = True qos.save() vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") atom = Option.objects.get(question=qed, text="Atom") sublime = Option.objects.get(question=qed, text="Sublime") windows = Option.objects.get(question=qos, text="Windows") self.assertEqual(unchoose(user, vim), None) self.assertEqual(unchoose(user, linux), None) self.assertEqual(unchoose(user, windows), None) self.assertEqual(choose(user, atom), None) self.assertEqual(choose(user, sublime), None) self.assertEqual(choose(user, windows), None) ed_choices = Choice.objects.filter(user=user, option__question=qed).order_by('id') os_choices = Choice.objects.filter(user=user, option__question=qos).order_by('id') self.assertEqual(ed_choices.count(), 2) self.assertEqual(os_choices.count(), 1) self.assertEqual(ed_choices[0].option_id, vim.id) self.assertEqual(ed_choices[1].option_id, atom.id) self.assertEqual(os_choices[0].option_id, linux.id) self.assertEqual(vote_count(vim), 1) self.assertEqual(vote_count(linux), 1) self.assertEqual(vote_count(atom), 1) self.assertEqual(vote_count(sublime), 0) self.assertEqual(vote_count(windows), 0)
def test_locked(self): # type: () -> None user = User.objects.get(username='******') qed = Question.objects.get(title="Text Editor") qos = Question.objects.get(title="Operating System") vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") atom = Option.objects.get(question=qed, text="Atom") self.assertEqual(choose(user, vim), True) self.assertEqual(choose(user, atom), True) self.assertEqual(choose(user, linux), True) qed.locked = True qed.save() qos.locked = True qos.save() vim = Option.objects.get(question=qed, text="Vim") linux = Option.objects.get(question=qos, text="Linux") atom = Option.objects.get(question=qed, text="Atom") sublime = Option.objects.get(question=qed, text="Sublime") windows = Option.objects.get(question=qos, text="Windows") self.assertEqual(unchoose(user, vim), None) self.assertEqual(unchoose(user, linux), None) self.assertEqual(unchoose(user, windows), None) self.assertEqual(choose(user, atom), None) self.assertEqual(choose(user, sublime), None) self.assertEqual(choose(user, windows), None) ed_choices = Choice.objects.filter(user=user, option__question=qed).order_by('id') os_choices = Choice.objects.filter(user=user, option__question=qos).order_by('id') self.assertEqual(ed_choices.count(), 2) self.assertEqual(os_choices.count(), 1) self.assertEqual(ed_choices[0].option_id, vim.id) self.assertEqual(ed_choices[1].option_id, atom.id) self.assertEqual(os_choices[0].option_id, linux.id) self.assertEqual(vote_count(vim), 1) self.assertEqual(vote_count(linux), 1) self.assertEqual(vote_count(atom), 1) self.assertEqual(vote_count(sublime), 0) self.assertEqual(vote_count(windows), 0)
client = Client() simple_urls = ('index', 'questions', 'options') for surl in simple_urls: context_dict['output_' + surl] = get_response_str(client.get(reverse('api:' + surl))) username = u"api_example_username" password = u"api_example_password" User.objects.filter(username=username).delete() user = User.objects.create_user(username=username, password=password) assert(client.login(username=username, password=password)) vim = Option.objects.get(text=u"Vim") atom = Option.objects.get(text=u"Atom") linux = Option.objects.get(text=u"Linux") choose(user, vim) choose(user, atom) choose(user, linux) context_dict["option_ids"] = text_type([vim.id, atom.id, linux.id]) response = client.get('/api/my-choices/') assert(response.status_code == 200) context_dict['output_my_choices'] = get_response_str(response) User.objects.filter(username=username).delete() context_dict['cookie'] = u'sessionid=csmdrzr8hisonw4uih5i3m1k70vhfprl' context_dict['output_login'] = u""" HTTP/1.0 200 OK
def vote(request): # type: (HttpRequest) -> HttpResponse # parse data if not request.body: return text_response("") content_type = request.META['CONTENT_TYPE'] data = get_parsed_post_data(request) zero_warn_response = text_response("has_zero_values", 400) invalid_format = text_response("invalid_format", 400) if is_form_content_type(content_type): try: choose_list = [OptionId(x) for x in data.getlist("choose")] unchoose_list = [OptionId(x) for x in data.getlist("unchoose")] except ValueError: return invalid_format elif content_type.startswith("application/json"): if isinstance(data, Sequence): try: olist = [int(x) for x in data] except ValueError: return invalid_format choose_list = [] unchoose_list = [] for oid in olist: if oid>0: choose_list.append(OptionId(oid)) elif oid<0: unchoose_list.append(OptionId(-oid)) else: return zero_warn_response elif isinstance(data, Mapping): choose_list = data.get("choose", []) unchoose_list = data.get("unchoose", []) if not (isinstance(choose_list, Sequence) and isinstance(unchoose_list, Sequence)): return invalid_format else: return invalid_format choose_set = set(choose_list) unchoose_set = set(unchoose_list) osets = (choose_set, unchoose_set) # check for 0, negative values and common values if choose_set.intersection(unchoose_set): return text_response("nonempty_intersection", 400) for oset in osets: if 0 in oset: return zero_warn_response for v in oset: if not isinstance(v, int): return invalid_format if v < 0: return text_response("has_negative_values", 400) # check if all oids are valid oids_in_db = get_all_oids_set() for oset in osets: if oset - oids_in_db: return text_response("has_invalid_values", 400) for oid in choose_set: choose(request.user, Option.objects.get(id=oid)) for oid in unchoose_set: unchoose(request.user, Option.objects.get(id=oid)) return text_response("")
simple_urls = ('index', 'questions', 'options') for surl in simple_urls: context_dict['output_' + surl] = get_response_str( client.get(reverse('api:' + surl))) username = u"api_example_username" password = u"api_example_password" User.objects.filter(username=username).delete() user = User.objects.create_user(username=username, password=password) assert (client.login(username=username, password=password)) vim = Option.objects.get(text=u"Vim") atom = Option.objects.get(text=u"Atom") linux = Option.objects.get(text=u"Linux") choose(user, vim) choose(user, atom) choose(user, linux) context_dict["option_ids"] = text_type([vim.id, atom.id, linux.id]) response = client.get('/api/my-choices/') assert (response.status_code == 200) context_dict['output_my_choices'] = get_response_str(response) User.objects.filter(username=username).delete() context_dict['cookie'] = u'sessionid=csmdrzr8hisonw4uih5i3m1k70vhfprl' context_dict['output_login'] = u""" HTTP/1.0 200 OK