Remove API to validate user access
[aaa.git] / aaa-shiro / impl / src / test / java / org / opendaylight / aaa / shiro / idm / rest / test / DomainHandlerTest.java
1 /*
2  * Copyright (c) 2016, 2017 Inocybe Technologies and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.aaa.shiro.idm.rest.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15
16 import java.util.HashMap;
17 import java.util.Map;
18 import javax.ws.rs.NotFoundException;
19 import javax.ws.rs.core.Response;
20 import org.junit.Test;
21 import org.opendaylight.aaa.api.model.Domain;
22 import org.opendaylight.aaa.api.model.Domains;
23 import org.opendaylight.aaa.api.model.IDMError;
24 import org.opendaylight.aaa.api.model.Roles;
25
26 public class DomainHandlerTest extends HandlerTest {
27
28     @Test
29     public void testDomainHandler() {
30         // check default domains
31         Domains domains = target("/v1/domains").request().get(Domains.class);
32         assertNotNull(domains);
33         assertEquals(1, domains.getDomains().size());
34         assertTrue(domains.getDomains().get(0).getName().equals("sdn"));
35
36         // check existing domain
37         Domain domain = target("/v1/domains/0").request().get(Domain.class);
38         assertNotNull(domain);
39         assertTrue(domain.getName().equals("sdn"));
40
41         // check not exist domain
42         try {
43             target("/v1/domains/5").request().get(IDMError.class);
44             fail("Should fail with 404!");
45         } catch (NotFoundException e) {
46             // expected
47         }
48
49         // check create domain
50         Map<String, String> domainData = new HashMap<>();
51         domainData.put("name", "dom1");
52         domainData.put("description", "test dom");
53         domainData.put("enabled", "true");
54         Response clientResponse = target("/v1/domains").request().post(entity(domainData));
55         assertEquals(201, clientResponse.getStatus());
56
57         // check update domain data
58         domainData.put("name", "dom1Update");
59         clientResponse = target("/v1/domains/1").request().put(entity(domainData));
60         assertEquals(200, clientResponse.getStatus());
61         domain = target("/v1/domains/1").request().get(Domain.class);
62         assertNotNull(domain);
63         assertTrue(domain.getName().equals("dom1Update"));
64
65         // check create grant
66         Map<String, String> grantData = new HashMap<>();
67         grantData.put("roleid", "1");
68         clientResponse = target("/v1/domains/1/users/0/roles").request().post(entity(grantData));
69         assertEquals(201, clientResponse.getStatus());
70
71         // check create existing grant
72         clientResponse = target("/v1/domains/1/users/0/roles").request().post(entity(grantData));
73         assertEquals(403, clientResponse.getStatus());
74
75         // check create grant with invalid domain id
76         clientResponse = target("/v1/domains/5/users/0/roles").request().post(entity(grantData));
77         assertEquals(404, clientResponse.getStatus());
78
79         // check get user (admin) roles
80         Roles usrRoles = target("/v1/domains/0/users/0/roles").request().get(Roles.class);
81         assertNotNull(usrRoles);
82         assertTrue(usrRoles.getRoles().size() > 1);
83
84         // check get invalid user roles
85         try {
86             target("/v1/domains/0/users/5/roles").request().get(IDMError.class);
87             fail("Should fail with 404!");
88         } catch (NotFoundException e) {
89             // expected
90         }
91
92         // check delete grant
93         clientResponse = target("/v1/domains/0/users/0/roles/0").request().delete();
94         assertEquals(204, clientResponse.getStatus());
95
96         // check delete grant for invalid domain
97         clientResponse = target("/v1/domains/3/users/0/roles/0").request().delete();
98         assertEquals(404, clientResponse.getStatus());
99
100         // check delete domain
101         clientResponse = target("/v1/domains/1").request().delete();
102         assertEquals(204, clientResponse.getStatus());
103
104         // check delete not existing domain
105         try {
106             target("/v1/domains/1").request().delete(IDMError.class);
107             fail("Should fail with 404!");
108         } catch (NotFoundException e) {
109             // expected
110         }
111
112         // Bug 8382: if a domain id is specified, 400 is returned
113         domainData = new HashMap<>();
114         domainData.put("name", "dom1");
115         domainData.put("description", "test dom");
116         domainData.put("domainid", "dom1");
117         domainData.put("enabled", "true");
118         clientResponse = target("/v1/domains").request().post(entity(domainData));
119         assertEquals(400, clientResponse.getStatus());
120
121         // Bug 8382: if a grant id is specified, 400 is returned
122         grantData = new HashMap<>();
123         grantData.put("roleid", "1");
124         grantData.put("grantid", "grantid");
125         clientResponse = target("/v1/domains/1/users/0/roles").request().post(entity(grantData));
126         assertEquals(400, clientResponse.getStatus());
127     }
128 }