~lutostag/ubuntu/trusty/maas/1.5.4+keystone

« back to all changes in this revision

Viewing changes to src/maasserver/tests/test_middleware.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-03-27 14:49:56 UTC
  • mto: (20.1.1 quantal) (1.2.1)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20120327144956-z5stunhc83bnnwsi
Tags: upstream-0.1+bzr363+dfsg
ImportĀ upstreamĀ versionĀ 0.1+bzr363+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
72
72
        exception = MAASAPINotFound("Huh?")
73
73
        self.assertIsNone(middleware.process_exception(request, exception))
74
74
 
75
 
    def test_ignores_unknown_exception(self):
76
 
        # An unknown exception is not processed by the middleware
77
 
        # (returns None).
78
 
        self.assertIsNone(
79
 
            self.process_exception(ValueError("Error occurred!")))
 
75
    def test_unknown_exception_generates_internal_server_error(self):
 
76
        # An unknown exception generates an internal server error with the
 
77
        # exception message.
 
78
        error_message = factory.getRandomString()
 
79
        response = self.process_exception(RuntimeError(error_message))
 
80
        self.assertEqual(
 
81
            (httplib.INTERNAL_SERVER_ERROR, error_message),
 
82
            (response.status_code, response.content))
80
83
 
81
84
    def test_reports_MAASAPIException_with_appropriate_api_error(self):
82
85
        class MyException(MAASAPIException):
83
86
            api_error = httplib.UNAUTHORIZED
84
87
 
85
 
        exception = MyException("Error occurred!")
 
88
        error_message = factory.getRandomString()
 
89
        exception = MyException(error_message)
86
90
        response = self.process_exception(exception)
87
91
        self.assertEqual(
88
 
            (httplib.UNAUTHORIZED, "Error occurred!"),
 
92
            (httplib.UNAUTHORIZED, error_message),
89
93
            (response.status_code, response.content))
90
94
 
91
95
    def test_renders_MAASAPIException_as_unicode(self):
99
103
            (response.status_code, response.content.decode('utf-8')))
100
104
 
101
105
    def test_reports_ValidationError_as_Bad_Request(self):
102
 
        response = self.process_exception(ValidationError("Validation Error"))
 
106
        error_message = factory.getRandomString()
 
107
        response = self.process_exception(ValidationError(error_message))
103
108
        self.assertEqual(
104
 
            (httplib.BAD_REQUEST, "Validation Error"),
 
109
            (httplib.BAD_REQUEST, error_message),
105
110
            (response.status_code, response.content))
106
111
 
107
112
    def test_returns_ValidationError_message_dict_as_json(self):
108
 
        exception = ValidationError("Error")
 
113
        exception = ValidationError(factory.getRandomString())
109
114
        exception_dict = {'hostname': 'invalid'}
110
115
        setattr(exception, 'message_dict', exception_dict)
111
116
        response = self.process_exception(exception)
118
123
    def test_handles_error_on_API(self):
119
124
        middleware = APIErrorsMiddleware()
120
125
        non_api_request = fake_request("/api/1.0/hello")
121
 
        exception = MAASAPINotFound("Have you looked under the couch?")
 
126
        error_message = factory.getRandomString()
 
127
        exception = MAASAPINotFound(error_message)
122
128
        response = middleware.process_exception(non_api_request, exception)
123
129
        self.assertEqual(
124
 
            (httplib.NOT_FOUND, "Have you looked under the couch?"),
 
130
            (httplib.NOT_FOUND, error_message),
125
131
            (response.status_code, response.content))
126
132
 
127
133
    def test_ignores_error_outside_API(self):
128
134
        middleware = APIErrorsMiddleware()
129
135
        non_api_request = fake_request("/middleware/api/hello")
130
 
        exception = MAASAPINotFound("Have you looked under the couch?")
 
136
        exception = MAASAPINotFound(factory.getRandomString())
131
137
        self.assertIsNone(
132
138
            middleware.process_exception(non_api_request, exception))
133
139