|
1 | 1 | import pytest, socket, requests, os
|
2 | 2 |
|
| 3 | +from secrets import BASEUSER_PW, BASEADMIN_PW |
| 4 | + |
| 5 | +jwt_token = '' |
| 6 | + |
3 | 7 | #
|
4 | 8 | # Run 'pytest' from the command line
|
5 | 9 | #
|
|
21 | 25 | SERVER_URL = "http://server:5000"
|
22 | 26 | IS_LOCAL = False
|
23 | 27 |
|
24 |
| -### DNS lookup tests |
| 28 | +### DNS lookup tests ############################## |
25 | 29 |
|
26 |
| -# Ensure DNS not resolving bad host names |
27 | 30 | def test_bad_dns():
|
| 31 | + """Verify DNS not resolving bad host names.""" |
28 | 32 | with pytest.raises(socket.gaierror):
|
29 | 33 | socket.getaddrinfo("bad_server_name_that_should_not_resolve", "5000")
|
30 | 34 |
|
31 | 35 |
|
32 |
| -# Do we get IPs for good names? |
33 |
| - |
34 |
| - |
35 | 36 | @pytest.mark.skipif(IS_LOCAL, reason="Not run when IS_LOCAL")
|
36 | 37 | def test_db_dns():
|
37 |
| - assert ( |
38 |
| - len(socket.getaddrinfo("db", "5000")) > 0 |
39 |
| - ) # getaddrinfo works for IPv4 and v6 |
| 38 | + """Verify we get IP for DB server.""" |
| 39 | + |
| 40 | + # getaddrinfo works for IPv4 and v6 |
| 41 | + try: |
| 42 | + gai = socket.getaddrinfo("db", "5000") |
| 43 | + except: |
| 44 | + pytest.fail('getaddrinfo() failed for db', pytrace=False) |
| 45 | + |
| 46 | + assert len(gai) > 0 |
40 | 47 |
|
41 | 48 |
|
42 | 49 | @pytest.mark.skipif(IS_LOCAL, reason="Not run when IS_LOCAL")
|
43 | 50 | def test_server_dns():
|
44 |
| - assert len(socket.getaddrinfo("server", "5000")) > 0 |
| 51 | + """Verify we get IP for API server.""" |
| 52 | + try: |
| 53 | + gai = socket.getaddrinfo("server", "5000") |
| 54 | + except socket.gaierror: |
| 55 | + pytest.fail('getaddrinfo() failed for server', pytrace=False) |
| 56 | + |
| 57 | + assert len(gai) > 0 |
45 | 58 |
|
46 | 59 |
|
47 | 60 | @pytest.mark.skipif(IS_LOCAL, reason="Not run when IS_LOCAL")
|
48 | 61 | def test_client_dns():
|
49 |
| - assert len(socket.getaddrinfo("client", "5000")) > 0 |
| 62 | + """Verify we get IP for client.""" |
| 63 | + try: |
| 64 | + gai = socket.getaddrinfo("client", "5000") |
| 65 | + except socket.gaierror: |
| 66 | + pytest.fail('getaddrinfo() failed for client', pytrace=False) |
50 | 67 |
|
| 68 | + assert len(gai) > 0 |
51 | 69 |
|
52 |
| -# Simple API tests |
| 70 | +# Simple API tests ################################################ |
53 | 71 |
|
54 | 72 |
|
55 | 73 | def test_currentFiles():
|
| 74 | + """360 view Current Files list""" |
56 | 75 | response = requests.get(SERVER_URL + "/api/listCurrentFiles")
|
57 | 76 | assert response.status_code == 200
|
58 | 77 |
|
59 | 78 |
|
60 | 79 | def test_statistics():
|
| 80 | + """360 view Statistics""" |
61 | 81 | response = requests.get(SERVER_URL + "/api/statistics")
|
62 | 82 | assert response.status_code == 200
|
63 | 83 |
|
| 84 | + |
| 85 | +def test_usertest(): |
| 86 | + """Verify liveness test works""" |
| 87 | + response = requests.get(SERVER_URL + "/api/user/test") |
| 88 | + assert response.status_code == 200 |
| 89 | + |
| 90 | +######## Dependent tests ################################# |
| 91 | + |
| 92 | +# Store info across tests |
| 93 | +class State: |
| 94 | + def __init__(self): |
| 95 | + self.state = {} |
| 96 | + |
| 97 | +@pytest.fixture(scope='session') |
| 98 | +def state() -> State: |
| 99 | + state = State() |
| 100 | + state.state['from_fixture'] = 0 |
| 101 | + return state |
| 102 | + |
| 103 | + |
| 104 | +def test_userlogin(state: State): |
| 105 | + """Verify base_user can log in/get JWT.""" |
| 106 | + data = {"username":"base_user", "password" : BASEUSER_PW} |
| 107 | + |
| 108 | + response = requests.post(SERVER_URL + "/api/user/login_json", json=data) |
| 109 | + assert response.status_code == 200 |
| 110 | + |
| 111 | + try: |
| 112 | + jwt_token = response.json()['access_token'] |
| 113 | + except: |
| 114 | + pytest.fail('Did not get access token', pytrace=False) |
| 115 | + |
| 116 | + assert len(jwt_token) > 16 |
| 117 | + |
| 118 | + # Store the token for later use |
| 119 | + state.state['base_user'] = jwt_token |
| 120 | + |
| 121 | + |
| 122 | +def test_useraccess(state: State): |
| 123 | + """Verify logged-in base_user can use JWT to access test_auth""" |
| 124 | + # Build auth string value including token from state |
| 125 | + b_string = 'Bearer ' + state.state['base_user'] |
| 126 | + |
| 127 | + assert len(b_string) > 24 |
| 128 | + |
| 129 | + auth_hdr = {'Authorization' : b_string} |
| 130 | + response = requests.get(SERVER_URL + "/api/user/test_auth", headers=auth_hdr) |
| 131 | + assert response.status_code == 200 |
| 132 | + |
| 133 | + |
| 134 | +def test_user_bad_pw(): |
| 135 | + """Verify base_user with bad pw fails""" |
| 136 | + data = {"username":"base_user", "password" : 'some_bad_password'} |
| 137 | + |
| 138 | + response = requests.post(SERVER_URL + "/api/user/login_json", json=data) |
| 139 | + assert response.status_code == 401 |
| 140 | + |
| 141 | + |
| 142 | +def test_inact_userblocked(state: State): |
| 143 | + """Verify base_user_inact can't login because marked inactive.""" |
| 144 | + # Same pw as base_user |
| 145 | + data = {"username":"base_user_inact", "password" : BASEUSER_PW} |
| 146 | + response = requests.post(SERVER_URL + "/api/user/login_json", json=data) |
| 147 | + assert response.status_code == 401 |
| 148 | + |
| 149 | + |
| 150 | +### Admin-level tests ###################################### |
| 151 | + |
| 152 | +def test_adminlogin(state: State): |
| 153 | + """Verify base_admin can log in/get JWT.""" |
| 154 | + data = {"username":"base_admin", "password" : BASEADMIN_PW} |
| 155 | + |
| 156 | + response = requests.post(SERVER_URL + "/api/user/login_json", json=data) |
| 157 | + assert response.status_code == 200 |
| 158 | + |
| 159 | + try: |
| 160 | + jwt_token = response.json()['access_token'] |
| 161 | + except: |
| 162 | + pytest.fail('Did not get access token', pytrace=False) |
| 163 | + |
| 164 | + assert len(jwt_token) > 16 |
| 165 | + |
| 166 | + # Store the token for later use |
| 167 | + state.state['base_admin'] = jwt_token |
| 168 | + |
| 169 | + |
| 170 | +def test_admingetusers(state: State): |
| 171 | + """Verify logged-in base_admin can use JWT to get user list """ |
| 172 | + # Build auth string value including token from state |
| 173 | + b_string = 'Bearer ' + state.state['base_admin'] |
| 174 | + |
| 175 | + assert len(b_string) > 24 |
| 176 | + |
| 177 | + auth_hdr = {'Authorization' : b_string} |
| 178 | + response = requests.get(SERVER_URL + "/api/admin/user/get_users", headers=auth_hdr) |
| 179 | + assert response.status_code == 200 |
| 180 | + |
| 181 | + userlist = response.json() |
| 182 | + assert len(userlist) > 1 |
| 183 | + |
| 184 | + |
| 185 | +def test_usergetusers(state: State): |
| 186 | + """Verify logged-in base_user *cannot* use JWT to get user list """ |
| 187 | + # Build auth string value including token from state |
| 188 | + b_string = 'Bearer ' + state.state['base_user'] |
| 189 | + |
| 190 | + assert len(b_string) > 24 |
| 191 | + |
| 192 | + auth_hdr = {'Authorization' : b_string} |
| 193 | + response = requests.get(SERVER_URL + "/api/admin/user/get_users", headers=auth_hdr) |
| 194 | + assert response.status_code == 403 |
0 commit comments