347
347
self.assertFalse(self.handler.auth.in_progress())
349
349
@mock.patch('uuid.uuid4', mock.Mock(return_value=mock.Mock(hex='DEFACED')))
350
@mock.patch('datetime.datetime',
352
**{'utcnow.return_value':
353
datetime.datetime(2013, 11, 21, 21)}))
354
351
def test_token_request(self):
355
352
# It supports requesting a token when authenticated.
356
353
self.handler.user.username = 'user'
479
476
self.assertEqual(expected, json.loads(response))
479
class ChangeSetTestMixin(object):
480
"""Define data for working with bundle change sets."""
482
content = yaml.safe_dump({
485
'charm': 'cs:trusty/django-42',
490
request = json.dumps({
493
'Request': 'GetChanges',
494
'Params': {'YAML': content},
501
'method': 'addCharm',
502
'args': ['cs:trusty/django-42'],
504
{'id': 'addService-1',
506
'args': ['cs:trusty/django-42', 'django', {}],
507
'requires': ['addCharm-0']},
513
class TestWebSocketHandlerChangeSet(
514
ChangeSetTestMixin, WebSocketHandlerTestMixin, helpers.WSSTestMixin,
515
LogTrapTestCase, AsyncHTTPSTestCase):
518
def test_changeset_request(self):
519
# The bundle change set is correctly returned.
520
write_message_path = 'guiserver.handlers.wrap_write_message'
521
with mock.patch(write_message_path) as mock_write_message:
522
handler = yield self.make_initialized_handler()
523
# Simulate the user is authenticated.
524
handler.user.is_authenticated = True
527
yield handler.on_message(self.request)
528
mock_write_message().assert_called_once_with(self.response)
531
def test_not_authenticated(self):
532
# The bundle change set support is only activated for logged in users.
533
client = yield self.make_client()
534
client.write_message(self.request)
535
expected_response = {
538
'Error': 'unauthorized access: no user logged in',
540
response = yield client.read_message()
541
self.assertEqual(expected_response, json.loads(response))
544
class TestSandboxHandler(
545
ChangeSetTestMixin, helpers.WSSTestMixin, LogTrapTestCase,
549
"""Return an application including the sandbox WebSocket handler."""
550
return web.Application([
551
(r'/ws', handlers.SandboxHandler, {}),
554
def make_client(self):
555
"""Return a WebSocket client ready to be connected to the server."""
556
return clients.websocket_connect(
557
self.io_loop, self.get_wss_url('/ws'), lambda message: None)
560
def test_changeset_request(self):
561
# The bundle change set is correctly returned.
563
client = yield self.make_client()
564
client.write_message(self.request)
565
response = yield client.read_message()
566
self.assertEqual(self.response, json.loads(response))
569
def test_unknown_request(self):
570
# A not implemented error is always returned for unknown requests.
571
client = yield self.make_client()
572
client.write_message(json.dumps({'Request': 'invalid'}))
573
expected_response = {'Error': 'not implemented (sandbox mode)'}
574
response = yield client.read_message()
575
self.assertEqual(expected_response, json.loads(response))
482
578
class TestIndexHandler(LogTrapTestCase, AsyncHTTPTestCase):