2 * Copyright (c) 2010-2023 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.hamcrest.MatcherAssert.assertThat;
17 import static org.junit.jupiter.api.Assertions.*;
19 import java.io.IOException;
20 import java.util.Collections;
21 import java.util.Dictionary;
23 import javax.ws.rs.client.Entity;
24 import javax.ws.rs.core.Response;
26 import org.glassfish.jersey.server.ResourceConfig;
27 import org.junit.jupiter.api.AfterEach;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.mockito.Mockito;
31 import org.openhab.io.hueemulation.internal.ConfigStore;
32 import org.openhab.io.hueemulation.internal.HueEmulationConfig;
33 import org.openhab.io.hueemulation.internal.dto.HueUnauthorizedConfig;
34 import org.openhab.io.hueemulation.internal.dto.response.HueResponse;
35 import org.openhab.io.hueemulation.internal.dto.response.HueSuccessResponseCreateUser;
36 import org.openhab.io.hueemulation.internal.rest.mocks.ConfigStoreWithoutMetadata;
38 import com.google.gson.Gson;
39 import com.google.gson.JsonElement;
40 import com.google.gson.JsonParser;
43 * Tests for various user management API endpoints.
45 * @author David Graeff - Initial contribution
47 public class UsersAndConfigTests {
49 ConfigurationAccess configurationAccess = new ConfigurationAccess();
51 CommonSetup commonSetup;
54 public void setUp() throws IOException {
55 commonSetup = new CommonSetup(false);
57 configurationAccess.cs = commonSetup.cs;
58 configurationAccess.userManagement = commonSetup.userManagement;
59 configurationAccess.configAdmin = commonSetup.configAdmin;
61 commonSetup.start(new ResourceConfig().registerInstances(configurationAccess));
65 public void tearDown() throws Exception {
66 commonSetup.dispose();
70 public void invalidUser() {
71 assertFalse(commonSetup.userManagement.authorizeUser("blub"));
75 public void validUser() {
76 assertTrue(commonSetup.userManagement.authorizeUser("testuser"));
80 public void configStoreRestartOnNoUUID() {
81 ConfigStore configStore = new ConfigStoreWithoutMetadata(commonSetup.networkAddressService,
82 commonSetup.configAdmin, commonSetup.scheduler);
85 assertThat(configStore.ds.config.uuid, is(""));
86 configStore.activate(Collections.emptyMap());
88 assertThat(configStore.getConfig().uuid, not(is("")));
89 // The config admin service was requested for the service config
90 Mockito.verify(commonSetup.configAdminConfig).getProperties();
91 Dictionary<String, Object> p = commonSetup.configAdminConfig.getProperties();
92 // And the service config was updated
93 assertThat(p.get(HueEmulationConfig.CONFIG_UUID), is(configStore.getConfig().uuid));
97 public void addUser() {
99 assertEquals(405, commonSetup.client.target(commonSetup.basePath).request().get().getStatus());
101 String body = "{'username':'testuser','devicetype':'app#device'}";
106 // Post should create a user, except: if linkbutton not enabled
107 response = commonSetup.client.target(commonSetup.basePath).request().post(Entity.json(body));
108 assertThat(response.getStatus(), is(200));
109 r = commonSetup.cs.gson.fromJson(response.readEntity(String.class), HueResponse[].class);
110 assertNotNull(r[0].error);
112 // Post should create a user
113 commonSetup.cs.ds.config.linkbutton = true;
114 response = commonSetup.client.target(commonSetup.basePath).request().post(Entity.json(body));
115 assertThat(response.getStatus(), is(200));
117 JsonElement e = JsonParser.parseString(response.readEntity(String.class)).getAsJsonArray().get(0);
118 e = e.getAsJsonObject().get("success");
119 HueSuccessResponseCreateUser rc = commonSetup.cs.gson.fromJson(e, HueSuccessResponseCreateUser.class);
121 assertThat(commonSetup.cs.ds.config.whitelist.get(rc.username).name, is("app#device"));
125 public void unauthorizedAccessTest() {
126 // Unauthorized config
128 response = commonSetup.client.target(commonSetup.basePath + "/config").request().get();
129 assertThat(response.getStatus(), is(200));
130 HueUnauthorizedConfig config = new Gson().fromJson(response.readEntity(String.class),
131 HueUnauthorizedConfig.class);
132 assertThat(config.bridgeid, is(commonSetup.cs.ds.config.bridgeid));
133 assertThat(config.name, is(commonSetup.cs.ds.config.name));
136 response = commonSetup.client.target(commonSetup.basePath + "/invalid/config").request().get();
137 assertThat(response.getStatus(), is(403));
138 assertThat(response.readEntity(String.class), containsString("error"));