~juju-gui-charmers/charms/trusty/juju-gui/trunk

« back to all changes in this revision

Viewing changes to server/guiserver/tests/test_handlers.py

  • Committer: Francesco Banconi
  • Date: 2015-06-04 17:26:08 UTC
  • mfrom: (60.13.186 develop-trunk)
  • Revision ID: francesco.banconi@canonical.com-20150604172608-7z11fddzae8qgcdg
New charm release with Juju GUI 1.4.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""Tests for the Juju GUI server handlers."""
18
18
 
19
 
import datetime
20
19
import json
21
20
import os
22
21
import shutil
38
37
    gen_test,
39
38
    LogTrapTestCase,
40
39
)
 
40
import yaml
41
41
 
42
42
from guiserver import (
43
43
    auth,
347
347
        self.assertFalse(self.handler.auth.in_progress())
348
348
 
349
349
    @mock.patch('uuid.uuid4', mock.Mock(return_value=mock.Mock(hex='DEFACED')))
350
 
    @mock.patch('datetime.datetime',
351
 
                mock.Mock(
352
 
                    **{'utcnow.return_value':
353
 
                       datetime.datetime(2013, 11, 21, 21)}))
 
350
    @helpers.patch_time
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))
480
477
 
481
478
 
 
479
class ChangeSetTestMixin(object):
 
480
    """Define data for working with bundle change sets."""
 
481
 
 
482
    content = yaml.safe_dump({
 
483
        'services': {
 
484
            'django': {
 
485
                'charm': 'cs:trusty/django-42',
 
486
                'num_units': 0,
 
487
            },
 
488
        },
 
489
    })
 
490
    request = json.dumps({
 
491
        'RequestId': 1,
 
492
        'Type': 'ChangeSet',
 
493
        'Request': 'GetChanges',
 
494
        'Params': {'YAML': content},
 
495
    })
 
496
    response = {
 
497
        'RequestId': 1,
 
498
        'Response': {
 
499
            'Changes': [
 
500
                {'id': 'addCharm-0',
 
501
                 'method': 'addCharm',
 
502
                 'args': ['cs:trusty/django-42'],
 
503
                 'requires': []},
 
504
                {'id': 'addService-1',
 
505
                 'method': 'deploy',
 
506
                 'args': ['cs:trusty/django-42', 'django', {}],
 
507
                 'requires': ['addCharm-0']},
 
508
            ]
 
509
        },
 
510
    }
 
511
 
 
512
 
 
513
class TestWebSocketHandlerChangeSet(
 
514
        ChangeSetTestMixin, WebSocketHandlerTestMixin, helpers.WSSTestMixin,
 
515
        LogTrapTestCase, AsyncHTTPSTestCase):
 
516
 
 
517
    @gen_test
 
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
 
525
        # Request changes.
 
526
 
 
527
        yield handler.on_message(self.request)
 
528
        mock_write_message().assert_called_once_with(self.response)
 
529
 
 
530
    @gen_test
 
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 = {
 
536
            'RequestId': 1,
 
537
            'Response': {},
 
538
            'Error': 'unauthorized access: no user logged in',
 
539
        }
 
540
        response = yield client.read_message()
 
541
        self.assertEqual(expected_response, json.loads(response))
 
542
 
 
543
 
 
544
class TestSandboxHandler(
 
545
        ChangeSetTestMixin, helpers.WSSTestMixin, LogTrapTestCase,
 
546
        AsyncHTTPSTestCase):
 
547
 
 
548
    def get_app(self):
 
549
        """Return an application including the sandbox WebSocket handler."""
 
550
        return web.Application([
 
551
            (r'/ws', handlers.SandboxHandler, {}),
 
552
        ])
 
553
 
 
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)
 
558
 
 
559
    @gen_test
 
560
    def test_changeset_request(self):
 
561
        # The bundle change set is correctly returned.
 
562
        self.maxDiff = None
 
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))
 
567
 
 
568
    @gen_test
 
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))
 
576
 
 
577
 
482
578
class TestIndexHandler(LogTrapTestCase, AsyncHTTPTestCase):
483
579
 
484
580
    def setUp(self):