]> git.basschouten.com Git - openhab-addons.git/blob
642cc6141383c96de47ea739b08de2c4a4589b9d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.io.hueemulation.internal.rest;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.junit.Assert.*;
17
18 import java.io.IOException;
19 import java.util.Collections;
20 import java.util.Dictionary;
21
22 import javax.ws.rs.client.Entity;
23 import javax.ws.rs.core.Response;
24
25 import org.glassfish.jersey.server.ResourceConfig;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mockito;
30 import org.openhab.io.hueemulation.internal.ConfigStore;
31 import org.openhab.io.hueemulation.internal.HueEmulationConfig;
32 import org.openhab.io.hueemulation.internal.dto.HueUnauthorizedConfig;
33 import org.openhab.io.hueemulation.internal.dto.response.HueResponse;
34 import org.openhab.io.hueemulation.internal.dto.response.HueSuccessResponseCreateUser;
35 import org.openhab.io.hueemulation.internal.rest.mocks.ConfigStoreWithoutMetadata;
36
37 import com.google.gson.Gson;
38 import com.google.gson.JsonElement;
39 import com.google.gson.JsonParser;
40
41 /**
42  * Tests for various user management API endpoints.
43  *
44  * @author David Graeff - Initial contribution
45  */
46 public class UsersAndConfigTests {
47
48     ConfigurationAccess configurationAccess = new ConfigurationAccess();
49
50     CommonSetup commonSetup;
51
52     @Before
53     public void setUp() throws IOException {
54         commonSetup = new CommonSetup(false);
55
56         configurationAccess.cs = commonSetup.cs;
57         configurationAccess.userManagement = commonSetup.userManagement;
58         configurationAccess.configAdmin = commonSetup.configAdmin;
59
60         commonSetup.start(new ResourceConfig().registerInstances(configurationAccess));
61     }
62
63     @After
64     public void tearDown() {
65         commonSetup.dispose();
66     }
67
68     @Test
69     public void invalidUser() {
70         assertFalse(commonSetup.userManagement.authorizeUser("blub"));
71     }
72
73     @Test
74     public void validUser() {
75         assertTrue(commonSetup.userManagement.authorizeUser("testuser"));
76     }
77
78     @Test
79     public void configStoreRestartOnNoUUID() {
80         ConfigStore configStore = new ConfigStoreWithoutMetadata(commonSetup.networkAddressService,
81                 commonSetup.configAdmin, commonSetup.scheduler);
82
83         // No uuid known yet
84         assertThat(configStore.ds.config.uuid, is(""));
85         configStore.activate(Collections.emptyMap());
86
87         assertThat(configStore.getConfig().uuid, not(is("")));
88         // The config admin service was requested for the service config
89         Mockito.verify(commonSetup.configAdminConfig).getProperties();
90         Dictionary<String, Object> p = commonSetup.configAdminConfig.getProperties();
91         // And the service config was updated
92         assertThat(p.get(HueEmulationConfig.CONFIG_UUID), is(configStore.getConfig().uuid));
93     }
94
95     @Test
96     public void addUser() {
97         // GET should fail
98         assertEquals(405, commonSetup.client.target(commonSetup.basePath).request().get().getStatus());
99
100         String body = "{'username':'testuser','devicetype':'app#device'}";
101
102         Response response;
103         HueResponse[] r;
104
105         // Post should create a user, except: if linkbutton not enabled
106         response = commonSetup.client.target(commonSetup.basePath).request().post(Entity.json(body));
107         assertThat(response.getStatus(), is(200));
108         r = commonSetup.cs.gson.fromJson(response.readEntity(String.class), HueResponse[].class);
109         assertNotNull(r[0].error);
110
111         // Post should create a user
112         commonSetup.cs.ds.config.linkbutton = true;
113         response = commonSetup.client.target(commonSetup.basePath).request().post(Entity.json(body));
114         assertThat(response.getStatus(), is(200));
115
116         JsonElement e = new JsonParser().parse(response.readEntity(String.class)).getAsJsonArray().get(0);
117         e = e.getAsJsonObject().get("success");
118         HueSuccessResponseCreateUser rc = commonSetup.cs.gson.fromJson(e, HueSuccessResponseCreateUser.class);
119         assertNotNull(rc);
120         assertThat(commonSetup.cs.ds.config.whitelist.get(rc.username).name, is("app#device"));
121     }
122
123     @Test
124     public void UnauthorizedAccessTest() {
125         // Unauthorized config
126         Response response;
127         response = commonSetup.client.target(commonSetup.basePath + "/config").request().get();
128         assertThat(response.getStatus(), is(200));
129         HueUnauthorizedConfig config = new Gson().fromJson(response.readEntity(String.class),
130                 HueUnauthorizedConfig.class);
131         assertThat(config.bridgeid, is(commonSetup.cs.ds.config.bridgeid));
132         assertThat(config.name, is(commonSetup.cs.ds.config.name));
133
134         // Invalid user name
135         response = commonSetup.client.target(commonSetup.basePath + "/invalid/config").request().get();
136         assertThat(response.getStatus(), is(403));
137         assertThat(response.readEntity(String.class), containsString("error"));
138     }
139 }