Exemplo n.º 1
0
 def setUp(self):
     self.user = User.objects.create_user('test_flag', '*****@*****.**','bqz_qux')
     self.user.save()
     self.toilet = Toilet()
     self.toilet.name = "New toilet"
     self.toilet.date = datetime.datetime.now()
     self.toilet.creator = self.user
     self.toilet.save()
     self.client = Client()
     self.flag = Flag()
     self.flag.name = "test flag"
     self.flag.explanation = "this is a test flag"
     self.flag.save()
Exemplo n.º 2
0
class FlagTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('test_flag', '*****@*****.**','bqz_qux')
        self.user.save()
        self.toilet = Toilet()
        self.toilet.name = "New toilet"
        self.toilet.date = datetime.datetime.now()
        self.toilet.creator = self.user
        self.toilet.save()
        self.client = Client()
        self.flag = Flag()
        self.flag.name = "test flag"
        self.flag.explanation = "this is a test flag"
        self.flag.save()
        
    #get flags all flags, EQ3
    def test_get_flags(self):
        self.client.login(username=self.user.username, password ='******')
        response = json.loads(self.client.post('/api/Flag/get/', {'filters' : json.dumps({})}).content)[0]
        self.assertEqual(response['pk'], self.flag.pk)

    #get flag by name EQ1
    def test_get_flag_by_name(self):
        response = json.loads(self.client.post('/api/Flag/get/', {'filters' : json.dumps({'name' : self.flag.name})}).content)[0]
        self.assertEqual(response['pk'], self.flag.pk)
    #EQ2
    def test_get_flag_name_does_not_exist(self):
        response = json.loads(self.client.post('/api/Flag/get/', {'filters' : json.dumps({'name' : 'foo bar'})}).content)
        #There are no flags with name foo bar
        self.assertEqual(len(response), 0)

    #Get rankings of the flag
    def test_get_rankings(self):
        flagRanking = FlagRanking(flag=self.flag, toilet = self.toilet, up_down_vote = 0)
        flagRanking.save()
        response = json.loads(self.client.post('/api/FlagRanking/get/', {'filters' : json.dumps({'toilet' : self.toilet.pk}) }).content)[0]
        self.assertEqual(response['fields']['up_down_vote'], 0)
        self.assertEqual(response['fields']['toilet'], self.toilet.pk)
        
    #Upvote a flag for the given toilet, EQ1
    def test_upvote_flag(self):
        self.client.login(username=self.user.username, password ='******')
        response = json.loads(self.client.post('/api/flag/upvote/', {'toilet_pk' : self.toilet.pk, 'flag_pk' : self.flag.pk}).content)[0]
        self.assertEqual(response['fields']['flag'], self.flag.pk)
        self.assertEqual(response['fields']['up_down_vote'], 1)

    #upvote a flag for a toilet that does not exist, EQ2
    def test_upvote_flag_no_toilet(self):
        self.client.login(username=self.user.username, password ='******')
        response = self.client.post('/api/flag/upvote/', {'toilet_pk' : 666, 'flag_pk' : self.flag.pk})
        #bad request 
        self.assertEqual(response.status_code, 400)
    #upvote a flag that does not exist, EQ3
    def test_upvote_flag_no_flag(self):
        self.client.login(username=self.user.username, password ='******')
        response = self.client.post('/api/flag/upvote/', {'toilet_pk' : self.toilet.pk, 'flag_pk' : 666})
        #bad request 
        self.assertEqual(response.status_code, 400)

    #Try to upvote the same flag on the same toilet twice, EQ4
    def test_upvote_twice(self):
        self.client.login(username=self.user.username, password ='******')
        response = json.loads(self.client.post('/api/flag/upvote/', {'toilet_pk' : self.toilet.pk, 'flag_pk' : self.flag.pk}).content)[0]
        self.assertEqual(response['fields']['flag'], self.flag.pk)
        self.assertEqual(response['fields']['up_down_vote'], 1)
        response = json.loads(self.client.post('/api/flag/upvote/', {'toilet_pk' : self.toilet.pk, 'flag_pk' : self.flag.pk}).content)[0]
        self.assertEqual(response['fields']['up_down_vote'], 1)
        
    #Try to upvote any flag while not logged in EQ5
    def test_upvote_not_logged_in(self):
        response = self.client.post('/api/flag/upvote/', {'toilet_pk' : self.toilet.pk, 'flag_pk' : self.flag.pk})
        self.assertEqual(response.status_code, 403)
        
    #Test downvote 
    def test_downvote(self):
        self.client.login(username=self.user.username, password ='******')
        flagRanking = FlagRanking(flag = self.flag, toilet = self.toilet, up_down_vote = 1)
        flagRanking.save()
        flagvote = FlagVote(user = self.user, flag = self.flag, toilet = self.toilet, vote = 1)
        flagvote.save()
        response = json.loads(self.client.post('/api/flag/downvote/', {'toilet_pk' : self.toilet.pk, 'flag_pk' : self.flag.pk}).content)[0]
        self.assertEqual(response['fields']['flag'], self.flag.pk)
        self.assertEqual(response['fields']['up_down_vote'], 0)
        
    #get flags when there are no flags
    def test_get_empty_flag_set(self):
        self.flag.delete()
        self.client.login(username=self.user.username, password ='******')
        response = json.loads(self.client.post('/api/Flag/get/', {'filters' : json.dumps({})}).content)
        self.assertEqual(len(response), 0)