2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.hueemulation.internal.rest;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.junit.Assert.*;
18 import java.io.IOException;
19 import java.util.Collections;
20 import java.util.Dictionary;
22 import javax.ws.rs.client.Entity;
23 import javax.ws.rs.core.Response;
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;
37 import com.google.gson.Gson;
38 import com.google.gson.JsonElement;
39 import com.google.gson.JsonParser;
42 * Tests for various user management API endpoints.
44 * @author David Graeff - Initial contribution
46 public class UsersAndConfigTests {
48 ConfigurationAccess configurationAccess = new ConfigurationAccess();
50 CommonSetup commonSetup;
53 public void setUp() throws IOException {
54 commonSetup = new CommonSetup(false);
56 configurationAccess.cs = commonSetup.cs;
57 configurationAccess.userManagement = commonSetup.userManagement;
58 configurationAccess.configAdmin = commonSetup.configAdmin;
60 commonSetup.start(new ResourceConfig().registerInstances(configurationAccess));
64 public void tearDown() {
65 commonSetup.dispose();
69 public void invalidUser() {
70 assertFalse(commonSetup.userManagement.authorizeUser("blub"));
74 public void validUser() {
75 assertTrue(commonSetup.userManagement.authorizeUser("testuser"));
79 public void configStoreRestartOnNoUUID() {
80 ConfigStore configStore = new ConfigStoreWithoutMetadata(commonSetup.networkAddressService,
81 commonSetup.configAdmin, commonSetup.scheduler);
84 assertThat(configStore.ds.config.uuid, is(""));
85 configStore.activate(Collections.emptyMap());
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));
96 public void addUser() {
98 assertEquals(405, commonSetup.client.target(commonSetup.basePath).request().get().getStatus());
100 String body = "{'username':'testuser','devicetype':'app#device'}";
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);
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));
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);
120 assertThat(commonSetup.cs.ds.config.whitelist.get(rc.username).name, is("app#device"));
124 public void UnauthorizedAccessTest() {
125 // Unauthorized config
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));
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"));