def rate(request): if not request.GET: return generate_response('fail', reason='not_get') username = request.GET['username'] if '.' in request.GET['rating']: return generate_response('fail', reason='incorrect_rating') try: rating = int(request.GET['rating']) except ValueError: return generate_response('fail', reason='incorrect_rating') if not User.objects.filter(username=username).exists(): return generate_response('fail', reason='user_not_exists') user = MyUser(username) try: user.rate(rating) except ValueError as e: return generate_response('fail', reason=str(e)) return generate_response('success')
class TestClass(TestCase): USERNAME = '******' TRACK_FOR_STAT = False DARK_THEME = False PASSWORD = '******' FIRST_NAME = 'test_user_name' LAST_NAME = 'test_user_surname' def create_user(self): self.user.create(track_for_stat=self.TRACK_FOR_STAT, dark_theme=self.DARK_THEME, password=self.PASSWORD, first_name=self.FIRST_NAME, last_name=self.LAST_NAME) def setUp(self): self.user = MyUser(self.USERNAME) def test_create_repeatedly(self): with self.assertRaises(UserIsAlreadyExistsException): self.create_user() self.create_user() def test_create_normally(self): self.create_user() self.assertEquals(self.user.user.username, self.USERNAME) self.assertEquals(self.user.user.userprofile.track_for_stat, self.TRACK_FOR_STAT) self.assertEquals(self.user.user.first_name, self.FIRST_NAME) self.assertEquals(self.user.user.last_name, self.LAST_NAME) def test_remove_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.remove() def test_manage_access_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.manage_access('control') def test_manage_access_normally(self): self.create_user() self.user.manage_access(['control']) self.assertTrue(self.user.has_access('control')) self.user.manage_access(['logs', 'moders']) self.assertTrue(self.user.has_access('logs')) self.assertTrue(self.user.has_access('moders')) def test_manage_rank_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.manage_rank('rank2') def test_manage_rank_nonexistent_rank(self): self.create_user() with self.assertRaises(RankNotFoundException): self.user.manage_rank('nonexistent_rank') def test_manage_rank_normally(self): self.create_user() rank_data = loads(self.user.user.userprofile.ranks) self.assertEquals(rank_data['rank'], 'rank0') self.user.manage_rank('rank4') rank_data = loads(self.user.user.userprofile.ranks) self.assertEquals(rank_data['rank'], 'rank4') def test_manage_additional_ranks_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.manage_additional_ranks(['paperwork']) def test_manage_additional_ranks_nonexistent_rank(self): self.create_user() with self.assertRaises(RankNotFoundException): self.user.manage_additional_ranks(['nonexistent_rank']) def test_manage_additional_ranks_normally(self): self.create_user() rank_data = loads(self.user.user.userprofile.ranks) self.assertEquals(len(rank_data['additional']), 0) self.user.manage_additional_ranks(['paperwork', 'expert']) rank_data = loads(self.user.user.userprofile.ranks) self.assertTrue('paperwork' in rank_data['additional']) self.assertTrue('expert' in rank_data['additional']) def test_manage_additional_ranks_save_automatic_assigned_ranks(self): self.create_user() self.user.manage_additional_ranks( ['expert', 'publicaccept', 'online500']) self.user.manage_additional_ranks(['paperwork']) self.assertEquals(self.user.get_additional_ranks_names_only(), ['paperwork', 'online500', 'publicaccept']) def test_toggle_tracking_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.toggle_tracking() def test_toggle_tracking_normally(self): self.create_user() self.user.toggle_tracking() self.assertEquals(self.user.user.userprofile.track_for_stat, False if self.TRACK_FOR_STAT else True) self.user.toggle_tracking() self.assertEquals(self.user.user.userprofile.track_for_stat, True if self.TRACK_FOR_STAT else False) def test_toggle_dark_theme_nonexistent(self): with self.assertRaises(UserNotFoundException): self.user.toggle_dark_theme() def test_toggle_dark_theme_normally(self): self.create_user() self.user.toggle_dark_theme() self.assertEquals(self.user.user.userprofile.dark_theme, False if self.DARK_THEME else True) self.user.toggle_dark_theme() self.assertEquals(self.user.user.userprofile.dark_theme, True if self.DARK_THEME else False) def test_get_tracked_for_stat(self): self.create_user() self.user.toggle_tracking() authorized_users = get_tracked_for_stat() self.assertEquals( authorized_users, {self.user.username: self.user.user.userprofile.color}) def test_rating_incorrect_string(self): self.create_user() with self.assertRaises(ValueError): self.user.rate('5') def test_rating_incorrect_float(self): self.create_user() with self.assertRaises(ValueError): self.user.rate(5.0) def test_rating_incorrect_more_than_five(self): self.create_user() with self.assertRaises(ValueError): self.user.rate(6) def test_rating_incorrect_less_than_one(self): self.create_user() with self.assertRaises(ValueError): self.user.rate(0) def test_rating_normal(self): self.create_user() self.assertEquals(self.user.get_rating(), 0.0) self.assertEquals(self.user.get_total_vote(), 0) self.assertEquals(self.user.get_vote_count(), 0) self.user.rate(3) self.assertEquals(self.user.get_rating(), 3.0) self.assertEquals(self.user.get_total_vote(), 3) self.assertEquals(self.user.get_vote_count(), 1) self.user.rate(5) self.assertEquals(self.user.get_rating(), 4.0) self.assertEquals(self.user.get_total_vote(), 8) self.assertEquals(self.user.get_vote_count(), 2) self.user.rate(2) self.assertEquals(self.user.get_rating(), 3.3) self.assertEquals(self.user.get_total_vote(), 10) self.assertEquals(self.user.get_vote_count(), 3) def test_add_online_negative(self): self.create_user() with self.assertRaises(ValueError): self.user.add_online(-5) def test_add_online_normal(self): self.create_user() self.assertEquals(self.user.get_online(), 0) self.user.add_online(5) self.assertEquals(self.user.get_online(), 5) self.user.add_online(10) self.assertEquals(self.user.get_online(), 15) def test_add_online_automatic_rank_assign(self): self.create_user() self.user.add_online(502) self.assertEquals(self.user.get_additional_ranks_names_only(), ['online500']) self.user.add_online(502) self.assertEquals(self.user.get_additional_ranks_names_only(), ['online1000']) self.user.manage_additional_ranks(['paperwork', 'expert']) self.user.add_online(1000) self.assertEquals(self.user.get_additional_ranks_names_only(), ['paperwork', 'expert', 'online2000'])