|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.cloudstack.cloudian; |
| 19 | + |
| 20 | +import java.io.UnsupportedEncodingException; |
| 21 | +import java.net.MalformedURLException; |
| 22 | +import java.net.URL; |
| 23 | +import java.net.URLDecoder; |
| 24 | +import java.util.HashMap; |
| 25 | +import org.apache.cloudstack.cloudian.client.CloudianUtils; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +public class CloudianUtilsTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testGenerateSSOUrl() { |
| 33 | + final String cmcUrlPath = "https://cmc.cloudian.com:8443/Cloudian/"; |
| 34 | + final String user = "abc-def-ghi"; |
| 35 | + final String group = "uvw-xyz"; |
| 36 | + final String ssoKey = "randomkey"; |
| 37 | + |
| 38 | + // test expectations |
| 39 | + final String expPath = "/Cloudian/ssosecurelogin.htm"; |
| 40 | + HashMap<String, String> expected = new HashMap(); |
| 41 | + expected.put("user", user); |
| 42 | + expected.put("group", group); |
| 43 | + expected.put("timestamp", null); // null value will not be checked by this test |
| 44 | + expected.put("signature", null); // null value will not be checked by this test |
| 45 | + expected.put("redirect", "/"); |
| 46 | + |
| 47 | + // Generated URL will be something like this |
| 48 | + // https://cmc.cloudian.com:8443/Cloudian/ssosecurelogin.htm?user=abc-def-ghi&group=uvw-xyz×tamp=1725937474949&signature=Wu1hjafeyE82mGwd1MIwrp5hPt4%3D&redirect=/ |
| 49 | + String output = CloudianUtils.generateSSOUrl(cmcUrlPath, user, group, ssoKey); |
| 50 | + Assert.assertNotNull(output); |
| 51 | + |
| 52 | + // Check main parts of the output URL |
| 53 | + URL url = null; |
| 54 | + try { |
| 55 | + url = new URL(output); |
| 56 | + } catch (MalformedURLException e) { |
| 57 | + Assert.fail("failed to parse URL: " + output); |
| 58 | + } |
| 59 | + String path = url.getPath(); |
| 60 | + Assert.assertEquals(expPath, path); |
| 61 | + |
| 62 | + // No easy way to check Query parameters in Java still? |
| 63 | + // Just do a rudementary check as we are in charge of the URL |
| 64 | + String query = url.getQuery(); |
| 65 | + String[] nameValues = query.split("&"); |
| 66 | + int matchedCount = 0; |
| 67 | + for(String nameValue : nameValues) { |
| 68 | + String[] nameValuePair = nameValue.split("=", 2); |
| 69 | + Assert.assertEquals(nameValue, 2, nameValuePair.length); |
| 70 | + String name = null; |
| 71 | + String value = null; |
| 72 | + try { |
| 73 | + name = URLDecoder.decode(nameValuePair[0], "UTF-8"); |
| 74 | + value = URLDecoder.decode(nameValuePair[1], "UTF-8"); |
| 75 | + } catch (UnsupportedEncodingException e) { |
| 76 | + Assert.fail("not expecting UTF-8 to fail"); |
| 77 | + } |
| 78 | + Assert.assertTrue(expected.containsKey(name)); |
| 79 | + matchedCount++; |
| 80 | + String expValue = expected.get(name); |
| 81 | + if (expValue != null) { |
| 82 | + Assert.assertEquals("Parameter " + name, expValue, value); |
| 83 | + } |
| 84 | + } |
| 85 | + Assert.assertEquals("Should be 5 query parameters", 5, matchedCount); |
| 86 | + } |
| 87 | +} |
0 commit comments