~sergiusens/ubuntu-push/rebuild

« back to all changes in this revision

Viewing changes to tests/autopilot/push_notifications/tests/__init__.py

  • Committer: Richard Huddie
  • Date: 2014-04-09 13:26:26 UTC
  • Revision ID: richard.huddie@canonical.com-20140409132626-i5n2yuqu8zqyhr15
added basic autopilot framework

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
# Copyright 2014 Canonical
 
3
#
 
4
# This program is free software: you can redistribute it and/or modify it
 
5
# under the terms of the GNU General Public License version 3, as published
 
6
# by the Free Software Foundation.
 
7
 
 
8
"""push-notifications autopilot tests."""
 
9
 
 
10
 
 
11
import configparser
 
12
 
 
13
from push_notifications import config
 
14
from autopilot.testcase import AutopilotTestCase
 
15
from autopilot.matchers import Eventually
 
16
from autopilot.platform import model
 
17
from testtools.matchers import Equals
 
18
 
 
19
 
 
20
class PushNotificationMessage:
 
21
    """
 
22
    Class to hold all the details required for a 
 
23
    push notification message
 
24
    """
 
25
    channel = ''
 
26
    expire_after = ''
 
27
    data = ''
 
28
 
 
29
 
 
30
class PushNotificationTestBase(AutopilotTestCase):
 
31
    """
 
32
    Base class for push notification test cases
 
33
    """
 
34
 
 
35
    PUSH_CLIENT_CONFIG_FILE = '~/.config/ubuntu-push-client/config.json'
 
36
    DEFAULT_DISPLAY_MESSAGE = 'There\'s an updated system image.'
 
37
    PUSH_MIME_TYPE = 'application/json'
 
38
    SECTION_DEFAULT = 'default'
 
39
    KEY_ENVIRONMENT = 'environment'
 
40
    KEY_SERVER_DEVICE_URL = 'push_server_device_url'
 
41
    KEY_SERVER_LISTENER_URL = 'push_server_listener_url'
 
42
 
 
43
    def setUp(self):
 
44
        """
 
45
        Start the client running with the correct server config
 
46
        """
 
47
        # Read the config data
 
48
        self.read_config_file()
 
49
        # Read the server device address
 
50
        server_device_address = self.get_push_server_device_address()
 
51
        # write server device address to the client config
 
52
        self.write_push_client_server_address(server_device_address)
 
53
        # restart the push client
 
54
        self.restart_push_client()
 
55
        # validate that the initialisation push message is displayed
 
56
        self.validate_push_message(self.DEFAULT_DISPLAY_MESSAGE)
 
57
        # setup
 
58
        super(PushNotificationTestBase, self).setUp()
 
59
 
 
60
 
 
61
    def read_config_file(self):
 
62
        """
 
63
        Read data from config file
 
64
        """
 
65
        config_file = config.get_config_file()
 
66
        self.config = configparser.ConfigParser()
 
67
        self.config.read(config_file)
 
68
        # read the name of the environment to use (local/remote)
 
69
        self.environment = self.config.get(self.SECTION_DEFAULT, self.KEY_ENVIRONMENT)
 
70
 
 
71
    def get_push_server_device_address(self):
 
72
        """
 
73
        Return the server device address from config file
 
74
        """
 
75
        return self.config.get(self.environment, self.KEY_SERVER_DEVICE_URL)
 
76
 
 
77
    def get_push_server_listener_address(self):
 
78
        """
 
79
        Return the server device address from config file
 
80
        """
 
81
        return self.config(self.environment, self.KEY_SERVER_LISTENER_URL)
 
82
 
 
83
    def restart_push_client(self):
 
84
        """
 
85
        Restart the push client
 
86
        """
 
87
 
 
88
    def write_push_client_server_address(self, server_address):
 
89
        """
 
90
        Write the server details to the push client config
 
91
        """
 
92
 
 
93
    def send_push_notification(self, server_address, json_data):
 
94
        """
 
95
        Send the specified push message to the server
 
96
        using an HTTP POST command
 
97
        """
 
98
 
 
99
    def format_json_data(self, push_message):
 
100
        """
 
101
        Return the json formatted encoding of push_message including:
 
102
        channel, data, expire_after
 
103
        """
 
104
 
 
105
    def validate_push_message(self, display_message, timeout=10):
 
106
        """
 
107
        Validate that a notification message is displayed on screen
 
108
        """
 
109
 
 
110