TestAuth.test_missing_credentials_fails()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
import base64
2
from . import integ_test_base
3
4
5
class TestAuth(integ_test_base.IntegTestBase):
6
    def setUp(self):
7
        super(TestAuth, self).setUp()
8
        self.payload = """{
9
                "data": { "_arg1": [1, 2] },
10
                "script": "return [x * 2 for x in _arg1]"
11
            }"""
12
13
    def _get_pwd_file(self) -> str:
14
        return "./tests/integration/resources/pwdfile.txt"
15
16
    def test_missing_credentials_fails(self):
17
        headers = {
18
            "Content-Type": "application/json",
19
            "TabPy-Client": "Integration tests for Auth",
20
        }
21
22
        conn = self._get_connection()
23
        conn.request("POST", "/evaluate", self.payload, headers)
24
        res = conn.getresponse()
25
26
        self.assertEqual(401, res.status)
27
28
    def test_invalid_password(self):
29
        headers = {
30
            "Content-Type": "application/json",
31
            "TabPy-Client": "Integration tests for Auth",
32
            "Authorization": "Basic "
33
            + base64.b64encode("user1:wrong_password".encode("utf-8")).decode("utf-8"),
34
        }
35
36
        conn = self._get_connection()
37
        conn.request("POST", "/evaluate", self.payload, headers)
38
        res = conn.getresponse()
39
40
        self.assertEqual(401, res.status)
41
42
    def test_invalid_username(self):
43
        # Uncomment the following line to preserve
44
        # test case output and other files (config, state, ect.)
45
        # in system temp folder.
46
        # self.set_delete_temp_folder(False)
47
48
        headers = {
49
            "Content-Type": "application/json",
50
            "TabPy-Client": "Integration tests for Auth",
51
            "Authorization": "Basic "
52
            + base64.b64encode("wrong_user:P@ssw0rd".encode("utf-8")).decode("utf-8"),
53
        }
54
55
        conn = self._get_connection()
56
        conn.request("POST", "/evaluate", self.payload, headers)
57
        res = conn.getresponse()
58
59
        self.assertEqual(401, res.status)
60
61
    def test_valid_credentials(self):
62
        headers = {
63
            "Content-Type": "application/json",
64
            "TabPy-Client": "Integration tests for Auth",
65
            "Authorization": "Basic "
66
            + base64.b64encode("user1:P@ssw0rd".encode("utf-8")).decode("utf-8"),
67
        }
68
69
        conn = self._get_connection()
70
        conn.request("POST", "/evaluate", self.payload, headers)
71
        res = conn.getresponse()
72
73
        self.assertEqual(200, res.status)
74