|
| 1 | +# Copyright 2022 IBM, Red Hat |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from pathlib import Path |
| 16 | +import sys |
| 17 | + |
| 18 | +parent = Path(__file__).resolve().parents[1] |
| 19 | +sys.path.append(str(parent) + "/src") |
| 20 | + |
| 21 | +from codeflare_sdk.cluster.cluster import Cluster, ClusterConfiguration |
| 22 | +from codeflare_sdk.cluster.auth import TokenAuthentication, PasswordUserAuthentication |
| 23 | +import openshift |
| 24 | +import pytest |
| 25 | + |
| 26 | + |
| 27 | +# For mocking openshift client results |
| 28 | +fake_res = openshift.Result("fake") |
| 29 | + |
| 30 | + |
| 31 | +def arg_side_effect(*args): |
| 32 | + fake_res.high_level_operation = args |
| 33 | + return fake_res |
| 34 | + |
| 35 | + |
| 36 | +def att_side_effect(self): |
| 37 | + return self.high_level_operation |
| 38 | + |
| 39 | + |
| 40 | +def test_token_auth_creation(): |
| 41 | + try: |
| 42 | + token_auth = TokenAuthentication() |
| 43 | + assert token_auth.token == None |
| 44 | + assert token_auth.server == None |
| 45 | + |
| 46 | + token_auth = TokenAuthentication("token") |
| 47 | + assert token_auth.token == "token" |
| 48 | + assert token_auth.server == None |
| 49 | + |
| 50 | + token_auth = TokenAuthentication("token", "server") |
| 51 | + assert token_auth.token == "token" |
| 52 | + assert token_auth.server == "server" |
| 53 | + |
| 54 | + token_auth = TokenAuthentication("token", server="server") |
| 55 | + assert token_auth.token == "token" |
| 56 | + assert token_auth.server == "server" |
| 57 | + |
| 58 | + token_auth = TokenAuthentication(token="token", server="server") |
| 59 | + assert token_auth.token == "token" |
| 60 | + assert token_auth.server == "server" |
| 61 | + |
| 62 | + except Exception: |
| 63 | + assert 0 == 1 |
| 64 | + |
| 65 | + |
| 66 | +def test_token_auth_login_logout(mocker): |
| 67 | + mocker.patch("openshift.invoke", side_effect=arg_side_effect) |
| 68 | + mock_res = mocker.patch.object(openshift.Result, "out") |
| 69 | + mock_res.side_effect = lambda: att_side_effect(fake_res) |
| 70 | + |
| 71 | + token_auth = TokenAuthentication(token="testtoken", server="testserver") |
| 72 | + assert token_auth.login() == ( |
| 73 | + "login", |
| 74 | + ["--token=testtoken", "--server=testserver:6443"], |
| 75 | + ) |
| 76 | + assert token_auth.logout() == ("logout",) |
| 77 | + |
| 78 | + |
| 79 | +def test_passwd_auth_creation(): |
| 80 | + try: |
| 81 | + passwd_auth = PasswordUserAuthentication() |
| 82 | + assert passwd_auth.username == None |
| 83 | + assert passwd_auth.password == None |
| 84 | + |
| 85 | + passwd_auth = PasswordUserAuthentication("user") |
| 86 | + assert passwd_auth.username == "user" |
| 87 | + assert passwd_auth.password == None |
| 88 | + |
| 89 | + passwd_auth = PasswordUserAuthentication("user", "passwd") |
| 90 | + assert passwd_auth.username == "user" |
| 91 | + assert passwd_auth.password == "passwd" |
| 92 | + |
| 93 | + passwd_auth = PasswordUserAuthentication("user", password="passwd") |
| 94 | + assert passwd_auth.username == "user" |
| 95 | + assert passwd_auth.password == "passwd" |
| 96 | + |
| 97 | + passwd_auth = PasswordUserAuthentication(username="user", password="passwd") |
| 98 | + assert passwd_auth.username == "user" |
| 99 | + assert passwd_auth.password == "passwd" |
| 100 | + |
| 101 | + except Exception: |
| 102 | + assert 0 == 1 |
| 103 | + |
| 104 | + |
| 105 | +def test_passwd_auth_login_logout(mocker): |
| 106 | + mocker.patch("openshift.invoke", side_effect=arg_side_effect) |
| 107 | + mocker.patch("openshift.login", side_effect=arg_side_effect) |
| 108 | + mock_res = mocker.patch.object(openshift.Result, "out") |
| 109 | + mock_res.side_effect = lambda: att_side_effect(fake_res) |
| 110 | + |
| 111 | + token_auth = PasswordUserAuthentication(username="user", password="passwd") |
| 112 | + assert token_auth.login() == ("user", "passwd") |
| 113 | + assert token_auth.logout() == ("logout",) |
0 commit comments