// Copyright 2017 The Nomulus Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package google.registry.webdriver; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static google.registry.testing.AppEngineRule.THE_REGISTRAR_GAE_USER_ID; import static google.registry.util.NetworkUtils.getExternalAddressOfLocalSystem; import static google.registry.util.NetworkUtils.pickUnusedPort; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.net.HostAndPort; import google.registry.request.auth.AuthenticatedRegistrarAccessor; import google.registry.server.Fixture; import google.registry.server.Route; import google.registry.server.TestServer; import google.registry.testing.AppEngineRule; import google.registry.testing.UserInfo; import java.net.URL; import java.net.UnknownHostException; import java.nio.file.Path; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.FutureTask; import java.util.concurrent.LinkedBlockingDeque; import javax.servlet.Filter; import org.junit.rules.ExternalResource; import org.junit.runner.Description; import org.junit.runners.model.Statement; /** * JUnit Rule that sets up and tears down {@link TestServer}. * *
Warning: App Engine testing environments are thread local. This rule will spawn that
* testing environment in a separate thread from your unit tests. Therefore any modifications you
* need to make to that testing environment (e.g. Datastore interactions) must be done through the
* {@link #runInAppEngineEnvironment(Callable)} method.
*/
public final class TestServerRule extends ExternalResource {
private final ImmutableList This is sort of a hack because we can't actually change the user itself, nor that user's GAE
* roles. Instead we created a GAE-admin user in the constructor and we "bypass the admin check"
* if we want that user to not be an admin.
*
* A better implementation would be to replace the AuthenticatedRegistrarAccessor - that way we
* can fully control the Roles the user has without relying on the implementation. But right now
* we don't have the ability to change injected values like that :/
*/
public void setIsAdmin(boolean isAdmin) {
AuthenticatedRegistrarAccessor.bypassAdminCheck = !isAdmin;
}
/** @see TestServer#getUrl(String) */
public URL getUrl(String path) {
return testServer.getUrl(path);
}
/**
* Runs arbitrary code inside server event loop thread.
*
* You should use this method when you want to do things like change Datastore, because the
* App Engine testing environment is thread-local.
*/
public This builder has three required fields: {@link #setRunfiles}, {@link #setRoutes}, and {@link
* #setFilters}.
*
*/
public static final class Builder {
private ImmutableMap This unfortunately cannot be changed by test methods.
*/
public Builder setEmail(String email) {
this.email = email;
return this;
}
/** Returns a new {@link TestServerRule} instance. */
public TestServerRule build() {
return new TestServerRule(
checkNotNull(this.runfiles),
checkNotNull(this.routes),
checkNotNull(this.filters),
checkNotNull(this.fixtures),
checkNotNull(this.email));
}
}
}