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_my_choices_empty(self): # type: () -> None user = User.objects.get(username='******') 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, [])
def test_questions(self): # type: () -> None response = self.client.get('/api/questions/') response_str = get_response_str(response) data = json.loads(response_str) for (id, qdict) in six.iteritems(data): ques = Question.objects.get(id=id) for (key, value) in six.iteritems(qdict): self.assertEqual(qdict[key], getattr(ques, key))
def do_test_basic_auth(test, username, password, status_code, output=None, separator=':'): # type: (TestCase, text_type, text_type, int, Optional[text_type], text_type) -> None credentials = username + separator + password smashed_credentials = b64encode(credentials.encode('utf-8')).decode('utf-8') auth_header = 'Basic ' + smashed_credentials response = test.client.get('/api/my-choices/', HTTP_AUTHORIZATION=auth_header) test.assertEqual(response.status_code, status_code) if output is not None: test.assertEqual(get_response_str(response), output)
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_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_options(self): # type: () -> None response = self.client.get('/api/options/') response_str = get_response_str(response) data = json.loads(response_str) for (id, odict) in six.iteritems(data): option = Option.objects.get(id=id) self.assertEqual(odict["text"], option.text) self.assertEqual(odict["question"], option.question_id) if option.question.show_count: self.assertEqual(odict["count"], vote_count(option)) self.assertEqual(odict["count"], 0) else: self.assertIsNone(odict["count"])
def do_test_register(test, username, password, status_code, output, content_type=None): # type: (TestCase, text_type, text_type, int, Optional[text_type], Optional[text_type]) -> None data = {"username": username, "password": password} prevcount = User.objects.filter(username=username).count() response = send_request(test.client.post, '/api/register/', data, content_type) test.assertEqual(response.status_code, status_code) if output is not None: test.assertEqual(get_response_str(response), output) newcount = User.objects.filter(username=username).count() if output == "success": test.assertEqual(newcount, 1) else: test.assertEqual(newcount, prevcount)
def do_test_basic_auth(test, username, password, status_code, output=None, separator=':'): # type: (TestCase, text_type, text_type, int, Optional[text_type], text_type) -> None credentials = username + separator + password smashed_credentials = b64encode( credentials.encode('utf-8')).decode('utf-8') auth_header = 'Basic ' + smashed_credentials response = test.client.get('/api/my-choices/', HTTP_AUTHORIZATION=auth_header) test.assertEqual(response.status_code, status_code) if output is not None: test.assertEqual(get_response_str(response), output)
context_dict = OrderedDict() # type: Dict[str, text_type] for x in endpoint_list: context_dict["url_" + x] = reverse('api:' + x) SITE_URL = u'http://localhost:8000' for (k, v) in list(context_dict.items()): if k.startswith('url_'): context_dict['full_' + k] = SITE_URL + v 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])
context_dict = OrderedDict() # type: Dict[str, text_type] for x in endpoint_list: context_dict["url_" + x] = reverse('api:' + x) SITE_URL = u'http://localhost:8000' for (k, v) in list(context_dict.items()): if k.startswith('url_'): context_dict['full_' + k] = SITE_URL + v 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])
def test_all_ques(self): # type: () -> None response = self.client.get('/api/') response_dict = json.loads(get_response_str(response)) file_dict = json.load(open(TEST_DATA_FILE)) self.assertEqual(response_dict, file_dict)
def test_my_choices_unauthed(self): # type: () -> None response = self.client.get('/api/my-choices/') self.assertEqual(response.status_code, 401) self.assertEqual(get_response_str(response), 'auth_missing')