Example #1
0
    def assert_standard_fails(self, url, args, data=dict(), method="get", login_required=True, authorized=True):
        """
        shortcut function for running standard tests:
            * assert_404
            * assert_401 for anonymous user
            * assert_403 for a user with no permissions

        @param url - url to test
        @param args - args for the url string
        @param data - dictionary of data to be passed to the request
        @param method - http method to be used
        @param login_required - run assert_401 test, default=True
        @param authorized - run assert_403 test for unauthorized user,
            default=True
        """
        # unauthenticated
        if login_required:
            self.assert_401(url, args, data, method)

        # unauthorized
        if authorized:
            unauthorized = UserTestMixin.create_user("unauthorized")
            self.assert_403(url, args, [unauthorized], data, method)

        # test 404s - replace each argument one at a time with a
        # nonexistent value
        self.assert_404(url, args, method=method)
Example #2
0
    def assert_view_values(self, url, args, data, fields, template=None, mime=None, tests=None, method="post"):
        """
        Tests fields that should raise an error for a specific type of invalid
        data is sent.  This is used for blackbox testing form validation via
        the view it is used in.

        @param url - url to test
        @param args - args for the url string
        @param data - dictionary of data to be passed to the request
        @param fields - list of dictionaries of invalid data combinations
        @param template - if given, template that responses should use
        @param mime - if given, mime for response
        @param tests - a function that executes additional tests
        on the responses from the client
        @param method - http method to be used
        """
        mime = mime if mime else "text/html; charset=utf-8"
        c = Client()
        client_method = getattr(c, method)
        superuser = UserTestMixin.create_user("superuser", is_superuser=True)

        self.assertTrue(c.login(username=superuser.username, password="******"))

        # check required fields
        for values in fields:
            data_ = data.copy()
            data_.update(values)
            response = client_method(url % args, data_)
            self.assertEqual(200, response.status_code)
            if template is not None:
                self.assertTemplateUsed(response, template)
            if mime is not None:
                self.assertEqual(response["content-type"], mime)
            if tests is not None:
                tests(superuser, response)
Example #3
0
    def assert_standard_fails(self,
                              url,
                              args,
                              data=dict(),
                              method='get',
                              login_required=True,
                              authorized=True):
        """
        shortcut function for running standard tests:
            * assert_404
            * assert_401 for anonymous user
            * assert_403 for a user with no permissions

        @param url - url to test
        @param args - args for the url string
        @param data - dictionary of data to be passed to the request
        @param method - http method to be used
        @param login_required - run assert_401 test, default=True
        @param authorized - run assert_403 test for unauthorized user,
            default=True
        """
        # unauthenticated
        if login_required:
            self.assert_401(url, args, data, method)

        # unauthorized
        if authorized:
            unauthorized = UserTestMixin.create_user('unauthorized')
            self.assert_403(url, args, [unauthorized], data, method)

        # test 404s - replace each argument one at a time with a
        # nonexistent value
        self.assert_404(url, args, method=method)
Example #4
0
    def assert_view_missing_fields(self,
                                   url,
                                   args,
                                   data,
                                   fields=None,
                                   template=None,
                                   mime=None,
                                   tests=None,
                                   method='post'):
        """
        Tests fields that should raise an error in a view, usually from form
        validation

        @param url - url to test
        @param args - args for the url string
        @param data - dictionary of data to be passed to the request
        @param fields - list of field keys that are required
        @param template - if given, template that responses should use
        @param mime - if given, mime for response
        @param tests - a function that executes additional tests
        on the responses from the client
        @param method - http method to be used
        """
        fields = data.keys if fields is None else fields
        mime = mime if mime else 'text/html; charset=utf-8'
        c = Client()
        client_method = getattr(c, method)
        superuser = UserTestMixin.create_user('superuser', is_superuser=True)

        self.assertTrue(c.login(username=superuser.username,
                                password='******'))

        # check required fields
        for name in fields:
            data_ = data.copy()
            del data_[name]

            response = client_method(url % args, data_)
            self.assertEqual(200, response.status_code)
            if template is not None:
                self.assertTemplateUsed(response, template)
            if mime is not None:
                self.assertEqual(response['content-type'], mime)
            if tests is not None:
                tests(superuser, response)
Example #5
0
    def assert_view_values(self,
                           url,
                           args,
                           data,
                           fields,
                           template=None,
                           mime=None,
                           tests=None,
                           method='post'):
        """
        Tests fields that should raise an error for a specific type of invalid
        data is sent.  This is used for blackbox testing form validation via
        the view it is used in.

        @param url - url to test
        @param args - args for the url string
        @param data - dictionary of data to be passed to the request
        @param fields - list of dictionaries of invalid data combinations
        @param template - if given, template that responses should use
        @param mime - if given, mime for response
        @param tests - a function that executes additional tests
        on the responses from the client
        @param method - http method to be used
        """
        mime = mime if mime else 'text/html; charset=utf-8'
        c = Client()
        client_method = getattr(c, method)
        superuser = UserTestMixin.create_user('superuser', is_superuser=True)

        self.assertTrue(c.login(username=superuser.username,
                                password='******'))

        # check required fields
        for values in fields:
            data_ = data.copy()
            data_.update(values)
            response = client_method(url % args, data_)
            self.assertEqual(200, response.status_code)
            if template is not None:
                self.assertTemplateUsed(response, template)
            if mime is not None:
                self.assertEqual(response['content-type'], mime)
            if tests is not None:
                tests(superuser, response)
Example #6
0
    def assert_404(self, url, args, data=dict(), method="get"):
        """
        Verifies that invalid url args will result in 404

        @param url - url to test
        @param args - args for the url string
        @param data - dictionary of data to be passed to the request
        @param method - http method to be used
        """
        c = Client()
        superuser = UserTestMixin.create_user("superuser", is_superuser=True)
        method = getattr(c, method)

        # test 404s - replace each argument one at a time
        # with a nonexistent value
        self.assertTrue(c.login(username=superuser.username, password="******"))
        for i in range(len(args)):
            temp_args = [arg for arg in args]
            temp_args[i] = "DOES.NOT.EXIST.WILL.FAIL"
            response = method(url % tuple(temp_args), data)
            self.assertEqual(404, response.status_code)
Example #7
0
    def assert_404(self, url, args, data=dict(), method='get'):
        """
        Verifies that invalid url args will result in 404

        @param url - url to test
        @param args - args for the url string
        @param data - dictionary of data to be passed to the request
        @param method - http method to be used
        """
        c = Client()
        superuser = UserTestMixin.create_user('superuser', is_superuser=True)
        method = getattr(c, method)

        # test 404s - replace each argument one at a time
        # with a nonexistent value
        self.assertTrue(c.login(username=superuser.username,
                                password='******'))
        for i in range(len(args)):
            temp_args = [arg for arg in args]
            temp_args[i] = 'DOES.NOT.EXIST.WILL.FAIL'
            response = method(url % tuple(temp_args), data)
            self.assertEqual(404, response.status_code)
Example #8
0
    def assert_view_missing_fields(
        self, url, args, data, fields=None, template=None, mime=None, tests=None, method="post"
    ):
        """
        Tests fields that should raise an error in a view, usually from form
        validation

        @param url - url to test
        @param args - args for the url string
        @param data - dictionary of data to be passed to the request
        @param fields - list of field keys that are required
        @param template - if given, template that responses should use
        @param mime - if given, mime for response
        @param tests - a function that executes additional tests
        on the responses from the client
        @param method - http method to be used
        """
        fields = data.keys if fields is None else fields
        mime = mime if mime else "text/html; charset=utf-8"
        c = Client()
        client_method = getattr(c, method)
        superuser = UserTestMixin.create_user("superuser", is_superuser=True)

        self.assertTrue(c.login(username=superuser.username, password="******"))

        # check required fields
        for name in fields:
            data_ = data.copy()
            del data_[name]

            response = client_method(url % args, data_)
            self.assertEqual(200, response.status_code)
            if template is not None:
                self.assertTemplateUsed(response, template)
            if mime is not None:
                self.assertEqual(response["content-type"], mime)
            if tests is not None:
                tests(superuser, response)