|
1 | | - |
2 | 1 | """ |
3 | | -Unit tests for authentication in the CLI. |
| 2 | +Unit tests for the Deis CLI auth commands. |
4 | 3 |
|
5 | | -Run these tests with "python -m unittest deis.tests.test_auth" |
6 | | -or all tests with "python -m unittest discover". |
7 | | -""" # pylint: disable=C0103,R0201,R0904 |
| 4 | +Run these tests with "python -m unittest client.tests.test_auth" |
| 5 | +or with "./manage.py test client.AuthTest". |
| 6 | +""" |
8 | 7 |
|
9 | | -import os |
10 | | -import unittest |
| 8 | +from __future__ import unicode_literals |
| 9 | +from unittest import TestCase |
11 | 10 |
|
12 | 11 | import pexpect |
13 | 12 |
|
| 13 | +from .utils import DEIS |
| 14 | +from .utils import DEIS_SERVER |
| 15 | +from .utils import setup |
| 16 | +from .utils import teardown |
14 | 17 |
|
15 | | -# Locate the 'of' executable script relative to this file. |
16 | | -CLI = os.path.abspath( |
17 | | - os.path.join(os.path.dirname(__file__), '..', 'deis')) |
18 | | - |
19 | | - |
20 | | -class TestLogin(unittest.TestCase): |
21 | 18 |
|
22 | | - """Test authentication in the CLI.""" |
| 19 | +class AuthTest(TestCase): |
23 | 20 |
|
24 | | - def setUp(self): |
25 | | - # TODO: set up the CLI/api/fixtures/tests.json...somehow |
26 | | - self.child = pexpect.spawn('{} login'.format(CLI)) |
| 21 | + @classmethod |
| 22 | + def setUpClass(cls): |
| 23 | + cls.username, cls.password, _ = setup() |
27 | 24 |
|
28 | | - def tearDown(self): |
29 | | - self.child = None |
| 25 | + @classmethod |
| 26 | + def tearDownClass(cls): |
| 27 | + teardown(cls.username, cls.password, None) |
30 | 28 |
|
31 | | - def test_good_login(self): |
32 | | - """Test that a valid login responds with a success message.""" |
33 | | - child = self.child |
34 | | - child.expect('username:') |
35 | | - child.sendline('autotest') |
36 | | - child.expect('password:') |
37 | | - child.sendline('password') |
38 | | - child.expect('Logged in as autotest.') |
39 | | - # call a protected API endpoint to ensure we were authenticated |
40 | | - child = self.child = pexpect.spawn('{} apps'.format(CLI)) |
41 | | - child.expect('^\S+ +\(.+\)') |
42 | | - |
43 | | - def test_bad_login(self): |
44 | | - """Test that an invalid login responds with a failure message.""" |
45 | | - child = self.child |
46 | | - child.expect('username:') |
47 | | - child.sendline('autotest') |
48 | | - child.expect('password:') |
49 | | - child.sendline('Pa55w0rd') |
50 | | - child.expect('Login failed.') |
51 | | - # call a protected API endpoint to ensure we get an unauth error |
52 | | - child = self.child = pexpect.spawn('{} apps'.format(CLI)) |
53 | | - child.expect("\('Error") |
| 29 | + def test_login(self): |
| 30 | + # log in the interactive way |
| 31 | + child = pexpect.spawn("{} login {}".format(DEIS, DEIS_SERVER)) |
| 32 | + child.expect('username: ') |
| 33 | + child.sendline(self.username) |
| 34 | + child.expect('password: ') |
| 35 | + child.sendline(self.password) |
| 36 | + child.expect("Logged in as {}".format(self.username)) |
| 37 | + child.expect(pexpect.EOF) |
54 | 38 |
|
55 | 39 | def test_logout(self): |
56 | | - child = self.child = pexpect.spawn('{} logout'.format(CLI)) |
57 | | - child.expect('Logged out.') |
58 | | - # call a protected API endpoint to ensure we get an unauth error |
59 | | - child = self.child = pexpect.spawn('{} apps'.format(CLI)) |
60 | | - child.expect("\('Error") |
61 | | - |
62 | | - |
63 | | -class TestHelp(unittest.TestCase): |
64 | | - |
65 | | - """Test that the client can document its own behavior.""" |
66 | | - |
67 | | - def test_version(self): |
68 | | - """Test that the client reports its help message.""" |
69 | | - child = pexpect.spawn('{} --help'.format(CLI)) |
70 | | - child.expect(r'Usage: .*number of proxies\s+$') |
71 | | - child = pexpect.spawn('{} -h'.format(CLI)) |
72 | | - child.expect(r'Usage: .*number of proxies\s+$') |
73 | | - child = pexpect.spawn('{} help'.format(CLI)) |
74 | | - child.expect(r'Usage: .*number of proxies\s+$') |
75 | | - |
76 | | - |
77 | | -class TestVersion(unittest.TestCase): |
78 | | - |
79 | | - """Test that the client can report its version string.""" |
80 | | - |
81 | | - def test_version(self): |
82 | | - """Test that the client reports its version string.""" |
83 | | - child = pexpect.spawn('{} --version'.format(CLI)) |
84 | | - child.expect('Deis CLI 0.0.1') |
85 | | - child = pexpect.spawn('{} -v'.format(CLI)) |
86 | | - child.expect('Deis CLI 0.0.1') |
| 40 | + child = pexpect.spawn("{} logout".format(DEIS)) |
| 41 | + child.expect('Logged out') |
| 42 | + # log in the one-liner way |
| 43 | + child = pexpect.spawn("{} login {} --username={} --password={}".format( |
| 44 | + DEIS, DEIS_SERVER, self.username, self.password)) |
| 45 | + child.expect("Logged in as {}".format(self.username)) |
| 46 | + child.expect(pexpect.EOF) |
0 commit comments