Welcome to mirror list, hosted at ThFree Co, Russian Federation.

activation_tests.py « tests - github.com/nextcloud/nextcloudpi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7e871e5952434be056e35d4cd72957572e67b961 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3

"""
Automatic testing for NextCloudPi

Copyleft 2017 by Ignacio Nunez Hernanz <nacho _a_t_ ownyourbits _d_o_t_ com>
GPL licensed (see LICENSE file in repository root).
Use at your own risk!

   ./activation_tests.py [IP]

More at https://ownyourbits.com
"""

import sys
import time
import traceback
import urllib
import os
import getopt
import configparser
import signal
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.options import Options
from selenium.common.exceptions import UnexpectedAlertPresentException
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import TimeoutException

suite_name = "activation tests"
test_cfg = 'test_cfg.txt'
test_log = 'test_log.txt'


class tc:
    "terminal colors"
    brown='\033[33m'
    yellow='\033[33;1m'
    green='\033[32m'
    red='\033[31m'
    normal='\033[0m'


def usage():
    "Print usage"
    print("usage: activation_tests.py [-t|--timeout <timeout>] [-h|--no-gui] [ip [nc-port [admin-port]]]")


class Test:
    title  = "test"

    def new(self, title):
        self.title = title
        print("[check] " + "{:16}".format(title), end=' ', flush = True)

    def check(self, expression):
        if expression:
            print(tc.green + "ok" + tc.normal)
            self.log("ok")
        else:
            print(tc.red + "error" + tc.normal)
            self.log("error")
            sys.exit(1)

    def report(self, title, expression):
        self.new(title)
        self.check(expression)

    def log(self, result):
        config = configparser.ConfigParser()
        if os.path.exists(test_log):
            config.read(test_log)
        if not config.has_section(suite_name):
            config[suite_name] = {}
        config[suite_name][self.title] = result
        with open(test_log, 'w') as logfile:
            config.write(logfile)


def is_element_present(driver, how, what):
    try: driver.find_element(by=how, value=what)
    except NoSuchElementException: return False
    return True


def signal_handler(sig, frame):
        sys.exit(0)


def test_activation(IP, nc_port, admin_port, options, wait_timeout=120):
    """ Activation process checks"""

    # activation page
    test = Test()
    driver = webdriver.Firefox(options=options)
    driver.implicitly_wait(5)
    test.new("activation opens")
    driver.get(f"https://{IP}:{nc_port}")
    test.check("NextCloudPi Activation" in driver.title)
    try:
        ncp_pass = driver.find_element(By.ID, "ncp-pwd").get_attribute("value")
        nc_pass = driver.find_element(By.ID, "nc-pwd").get_attribute("value")

        config = configparser.ConfigParser()
        if not config.has_section('credentials'):
            config['credentials'] = {}
        config['credentials']['ncp_user' ] = 'ncp'
        config['credentials']['ncp_pass' ] = ncp_pass
        config['credentials']['nc_user'  ] = 'ncp'
        config['credentials']['nc_pass'  ] = nc_pass
        with open(test_cfg, 'w') as configfile:
            config.write(configfile)

        driver.find_element(By.ID, "activate-ncp").click()
        test.report("activation click", True)
    except:
        ncp_pass = ""
        test.report("activation click", False)

    test.new("activation ends")
    try:
        wait = WebDriverWait(driver, wait_timeout)
        wait.until(EC.text_to_be_present_in_element((By.ID, 'error-box'), "ACTIVATION SUCCESSFUL"))
        test.check(True)
    except TimeoutException:
        test.check(False)
    except:
        test.check(True)
    try:
        driver.close()
    except Exception as e:
        traceback.print_exception(e)
        print(f"Could not close driver: {e}")

    # ncp-web
    test.new("ncp-web")
    driver = webdriver.Firefox(options=options)
    driver.implicitly_wait(30)
    try:
        driver.get(f"https://ncp:{urllib.parse.quote_plus(ncp_pass)}@{IP}:{admin_port}")
    except UnexpectedAlertPresentException:
        pass
    except Exception as e:
        print(f"WARN: Exception while attempting to get ncp-web: '{e}'")
        raise e
    test.check("NextCloudPi Panel" in driver.title)
    test.report("first run wizard", is_element_present(driver, By.ID, "first-run-wizard"))

    driver.close()


if __name__ == "__main__":
    signal.signal(signal.SIGINT, signal_handler)

    # parse options
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'ht:', ['help', 'timeout=', 'no-gui'])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    arg_timeout = 120
    options = Options()
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit(2)
        elif opt == '--no-gui':
            options.headless = True
        elif opt in ('-t', '--timeout'):
            arg_timeout = int(arg)
        else:
            usage()
            sys.exit(2)

    # test

    IP = args[0] if len(args) > 0 else 'localhost'
    nc_port = args[1] if len(args) > 1 else "443"
    admin_port = args[2] if len(args) > 2 else "4443"
    print("Activation tests " + tc.yellow + IP + tc.normal)
    print("---------------------------")

    test_activation(IP, nc_port, admin_port, options, arg_timeout)

# License
#
# This script is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This script is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this script; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA