コード例 #1
0
ファイル: tests.py プロジェクト: schocco/mds-web
class ValidationTestCase(TestCase):
    """
    Make sure scale objects are properly validated
    """
    def setUp(self):
        self.user = User.objects.create_user("username", '*****@*****.**', "userpw")
        # create a DH trail
        self.t1 = Trail(name = "Testtrail1", owner = self.user, type="dh")
        self.t1.waypoints = MultiLineString(LineString((48.75118072, 8.539638519, 712),
                                  (48.75176078, 8.541011810, 696),
                                  (48.75133635, 8.545153141, 556),
                                  (48.75067140, 8.545582294, 531)))
        self.t1.save()
        # and an XC trail
        self.t2 = Trail(name = "Testtrail1", owner = self.user, type="xc")
        self.t2.waypoints = MultiLineString(LineString((48.75118072, 8.539638519, 712),
                                  (48.75176078, 8.541011810, 696),
                                  (48.75133635, 8.545153141, 556),
                                  (48.75067140, 8.545582294, 731)))
        self.t2.save()

        self.udh_data = {
            "avg_difficulty": 5,
            "max_difficulty": 3,
            "avg_slope": 10,
            "total_length": 3000,
            "trail": self.t1.pk
        }
        self.uxc_data = {
            "avg_difficulty": 5,
            "max_difficulty": 3,
            "max_slope_uh": 10,
            "total_length": 3000,
            "total_ascent": 500,
            "trail": self.t2.pk
        }

    def test_avg_max_validation(self):
        """
        Makes sure that avg is <= to max difficulty on UDH and UXC scales.
        """
        form = UXCscaleForm(self.uxc_data)
        self.assertFalse(form.is_valid(), "average must not be higher than maximum")
        form = UDHscaleForm(self.udh_data)
        self.assertFalse(form.is_valid(), "average must not be higher than maximum")
        self.udh_data["max_difficulty"] = 5
        form = UDHscaleForm(self.udh_data)
        self.assertTrue(form.is_valid(), msg=form.errors)

    def test_missing_field(self):
        self.udh_data.pop("avg_slope")
        form = UDHscaleForm(self.udh_data)
        self.assertFalse(form.is_valid(), "all fields should be mandatory")
コード例 #2
0
ファイル: tests.py プロジェクト: schocco/mds-web
class AuthTestCase(ResourceTestCase, SessionAuthMixin):
    '''
    Tests creation and removal of scale objects via the API.
    '''
    def setUp(self):
        super(AuthTestCase, self).setUp()

        # Create users
        self.username = '******'
        self.password = '******'
        self.user = User.objects.create_user(self.username, '*****@*****.**', self.password)
        self.username2 = "koala"
        self.user2 = User.objects.create_user(self.username2, "*****@*****.**", self.password)

        # create a trail
        self.t1 = Trail(name = "Testtrail1", owner = self.user)
        self.t1.waypoints = MultiLineString(LineString((48.75118072, 8.539638519, 712),
                                  (48.75176078, 8.541011810, 696),
                                  (48.75133635, 8.545153141, 556),
                                  (48.75067140, 8.545582294, 531)))
        self.t1.save()
        self.t1_url = reverse('api_dispatch_detail', kwargs={'resource_name':'trails', 'api_name':'v1', "pk": self.t1.id})
        self.list_url = reverse('api_dispatch_list', kwargs={'resource_name':'udh-scale', 'api_name':'v1'})
        self.post_data = {
            "avg_difficulty": 2,
            "avg_slope": 8,
            "max_difficulty": 3,
            "total_length": 2000,
            "trail": self.t1_url
        }

    def test_unauthenticated_save(self):
        'Unauthenticated users must not create score objects'
        self.assertHttpUnauthorized(self.api_client.post(self.list_url, format='json', data=self.post_data))

    def test_unauthorized_save(self):
        """
        Users may only rate their own trails.
        """
        self.login(self.username2, self.password)
        response = self.api_client.post(self.list_url, format='json', data=self.post_data)
        self.assertHttpUnauthorized(response)
        response = self.api_client.delete(self.t1_url, format="json")
        self.assertHttpUnauthorized(response)
        self.logout()

    def test_authenticated_save(self):
        """
        Authenticted users should be able to create ratings for their own trails.
        Returned data should match posted data.
        :return:
        """
        self.login(self.username, self.password)
        response = self.api_client.post(self.list_url, format='json', data=self.post_data)
        self.assertHttpCreated(response)
        self.assertValidJSON(response.content)
        scale = json.loads(response.content)
        to_mscale = MscaleFieldMixin().to_mscale
        self.assertEqual(to_mscale(scale["avg_difficulty"]).number, to_mscale(self.post_data["avg_difficulty"]).number)
        self.assertEqual(to_mscale(scale["max_difficulty"]).number, to_mscale(self.post_data["max_difficulty"]).number)
        self.logout()