|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | + /* |
| 21 | + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. |
| 22 | + */ |
| 23 | +package org.opensolaris.opengrok.authorization; |
| 24 | + |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.TreeMap; |
| 28 | +import java.util.TreeSet; |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | +import org.opensolaris.opengrok.configuration.Group; |
| 33 | +import org.opensolaris.opengrok.configuration.Project; |
| 34 | +import org.opensolaris.opengrok.configuration.RuntimeEnvironment; |
| 35 | + |
| 36 | +import static org.junit.Assert.assertEquals; |
| 37 | + |
| 38 | +/** |
| 39 | + * |
| 40 | + * @author Krystof Tulinger |
| 41 | + */ |
| 42 | +public class AuthorizationStackTest { |
| 43 | + |
| 44 | + private Set<Group> envGroups; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUp() { |
| 48 | + RuntimeEnvironment env = RuntimeEnvironment.getInstance(); |
| 49 | + envGroups = env.getGroups(); |
| 50 | + env.setGroups(new TreeSet<>()); |
| 51 | + } |
| 52 | + |
| 53 | + @After |
| 54 | + public void tearDown() { |
| 55 | + RuntimeEnvironment.getInstance().setGroups(envGroups); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testForGroupsAndForProjectsDiscovery() { |
| 60 | + Group g1, g2, g3; |
| 61 | + AuthorizationEntity stack; |
| 62 | + |
| 63 | + RuntimeEnvironment env = RuntimeEnvironment.getInstance(); |
| 64 | + |
| 65 | + /** |
| 66 | + * Structure<br> |
| 67 | + * <pre> |
| 68 | + * G1 + P1 |
| 69 | + * + P2 |
| 70 | + * + P3 |
| 71 | + * + G2 |
| 72 | + * + P4 |
| 73 | + * + P5 |
| 74 | + * + P6 |
| 75 | + * + P7 |
| 76 | + * G3 + P8 |
| 77 | + * + P9 |
| 78 | + * </pre> |
| 79 | + */ |
| 80 | + g1 = new Group(); |
| 81 | + g1.setName("group 1"); |
| 82 | + g1.addProject(new Project("project 1")); |
| 83 | + g1.addProject(new Project("project 2")); |
| 84 | + g1.addProject(new Project("project 3")); |
| 85 | + env.getGroups().add(g1); |
| 86 | + g2 = new Group(); |
| 87 | + g2.setName("group 2"); |
| 88 | + g2.addProject(new Project("project 4")); |
| 89 | + g2.addProject(new Project("project 5")); |
| 90 | + g2.addProject(new Project("project 6")); |
| 91 | + g2.addProject(new Project("project 7")); |
| 92 | + g1.addGroup(g2); |
| 93 | + env.getGroups().add(g2); |
| 94 | + g3 = new Group(); |
| 95 | + g3.setName("group 3"); |
| 96 | + g3.addProject(new Project("project 8")); |
| 97 | + g3.addProject(new Project("project 9")); |
| 98 | + env.getGroups().add(g3); |
| 99 | + |
| 100 | + // add g1 and all descendants their projects |
| 101 | + stack = new AuthorizationStack(AuthControlFlag.REQUIRED, ""); |
| 102 | + stack.setForGroups(new TreeSet<>()); |
| 103 | + stack.setForGroups("group 1"); |
| 104 | + stack.load(new TreeMap<>()); |
| 105 | + |
| 106 | + assertEquals(new TreeSet<>(Arrays.asList(new String[]{"group 1", "group 2"})), stack.forGroups()); |
| 107 | + assertEquals(new TreeSet<>(Arrays.asList(new String[]{"project 1", "project 2", "project 3", |
| 108 | + "project 4", "project 5", "project 6", "project 7"})), stack.forProjects()); |
| 109 | + |
| 110 | + // add group2, its parent g1 and g2 projects |
| 111 | + stack = new AuthorizationStack(AuthControlFlag.REQUIRED, ""); |
| 112 | + stack.setForGroups(new TreeSet<>()); |
| 113 | + stack.setForGroups("group 2"); |
| 114 | + stack.load(new TreeMap<>()); |
| 115 | + |
| 116 | + assertEquals(new TreeSet<>(Arrays.asList(new String[]{"group 1", "group 2"})), stack.forGroups()); |
| 117 | + assertEquals(new TreeSet<>(Arrays.asList(new String[]{"project 4", "project 5", "project 6", "project 7"})), stack.forProjects()); |
| 118 | + |
| 119 | + // add only g3 and its projects |
| 120 | + stack = new AuthorizationStack(AuthControlFlag.REQUIRED, ""); |
| 121 | + stack.setForGroups(new TreeSet<>()); |
| 122 | + stack.setForGroups("group 3"); |
| 123 | + stack.load(new TreeMap<>()); |
| 124 | + |
| 125 | + assertEquals(new TreeSet<>(Arrays.asList(new String[]{"group 3"})), stack.forGroups()); |
| 126 | + assertEquals(new TreeSet<>(Arrays.asList(new String[]{"project 8", "project 9"})), stack.forProjects()); |
| 127 | + } |
| 128 | +} |
0 commit comments