Clean up the tattered shreds of SessionMetadata

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=125334811
This commit is contained in:
cgoldfeder 2016-06-20 07:26:00 -07:00 committed by Ben McIlwain
parent 2a3a3fbc30
commit bb82f5bc05
11 changed files with 169 additions and 185 deletions

View file

@ -22,9 +22,9 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import google.registry.testing.AppEngineRule;
import google.registry.testing.FakeHttpSession;
import google.registry.testing.ShardableTestCase;
import google.registry.testing.UserInfo;
import google.registry.util.BasicHttpSession;
import org.junit.Rule;
import org.junit.Test;
@ -47,7 +47,7 @@ public class EppConsoleActionTest extends ShardableTestCase {
public void doTest() {
EppConsoleAction action = new EppConsoleAction();
action.inputXmlBytes = INPUT_XML_BYTES;
action.session = new BasicHttpSession();
action.session = new FakeHttpSession();
action.session.setAttribute("CLIENT_ID", "ClientIdentifier");
action.eppRequestHandler = mock(EppRequestHandler.class);
action.run();

View file

@ -28,10 +28,10 @@ import com.google.common.net.MediaType;
import google.registry.model.ofy.Ofy;
import google.registry.monitoring.whitebox.EppMetrics;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeHttpSession;
import google.registry.testing.FakeResponse;
import google.registry.testing.InjectRule;
import google.registry.testing.ShardableTestCase;
import google.registry.testing.TestSessionMetadata;
import org.joda.time.DateTime;
import org.junit.Before;
@ -49,7 +49,7 @@ public class EppTestCase extends ShardableTestCase {
private final FakeClock clock = new FakeClock();
private TestSessionMetadata sessionMetadata;
private SessionMetadata sessionMetadata;
private TransportCredentials credentials = new PasswordOnlyTransportCredentials();
private boolean isSuperuser;
@ -94,12 +94,16 @@ public class EppTestCase extends ShardableTestCase {
String expectedOutput =
loadFileWithSubstitutions(getClass(), outputFilename, outputSubstitutions);
if (sessionMetadata == null) {
sessionMetadata = new TestSessionMetadata();
sessionMetadata = new HttpSessionMetadata(new FakeHttpSession()) {
@Override
public void invalidate() {
// When a session is invalidated, reset the sessionMetadata field.
super.invalidate();
EppTestCase.this.sessionMetadata = null;
}
};
}
String actualOutput = executeXmlCommand(input);
if (!sessionMetadata.isValid()) {
sessionMetadata = null;
}
assertXmlEqualsWithMessage(
expectedOutput,
actualOutput,

View file

@ -23,8 +23,8 @@ import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import google.registry.testing.FakeHttpSession;
import google.registry.testing.ShardableTestCase;
import google.registry.util.BasicHttpSession;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -43,7 +43,7 @@ public class EppTlsActionTest extends ShardableTestCase {
action.inputXmlBytes = INPUT_XML_BYTES;
action.tlsCredentials = mock(TlsCredentials.class);
when(action.tlsCredentials.hasSni()).thenReturn(true);
action.session = new BasicHttpSession();
action.session = new FakeHttpSession();
action.session.setAttribute("CLIENT_ID", "ClientIdentifier");
action.eppRequestHandler = mock(EppRequestHandler.class);
action.run();

View file

@ -49,8 +49,8 @@ import google.registry.model.tmch.ClaimsListShard.ClaimsListSingleton;
import google.registry.testing.AppEngineRule;
import google.registry.testing.EppLoader;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeHttpSession;
import google.registry.testing.InjectRule;
import google.registry.testing.TestSessionMetadata;
import google.registry.util.TypeUtils.TypeInstantiator;
import google.registry.xml.ValidationMode;
@ -95,7 +95,7 @@ public abstract class FlowTestCase<F extends Flow> {
@Before
public void init() throws Exception {
sessionMetadata = new TestSessionMetadata();
sessionMetadata = new HttpSessionMetadata(new FakeHttpSession());
sessionMetadata.setClientId("TheRegistrar");
sessionMetadata.setServiceExtensionUris(ProtocolDefinition.getVisibleServiceExtensionUris());
ofy().saveWithoutBackup().entity(new ClaimsListSingleton()).now();

View file

@ -32,6 +32,7 @@ import com.googlecode.objectify.Key;
import google.registry.flows.EppRequestSource;
import google.registry.flows.FlowRunner;
import google.registry.flows.HttpSessionMetadata;
import google.registry.flows.PasswordOnlyTransportCredentials;
import google.registry.flows.SessionMetadata;
import google.registry.model.domain.DomainResource;
@ -42,8 +43,8 @@ import google.registry.testing.AppEngineRule;
import google.registry.testing.EppLoader;
import google.registry.testing.ExceptionRule;
import google.registry.testing.FakeClock;
import google.registry.testing.FakeHttpSession;
import google.registry.testing.InjectRule;
import google.registry.testing.TestSessionMetadata;
import org.joda.time.DateTime;
import org.joda.time.Duration;
@ -79,7 +80,7 @@ public class EppResourceUtilsTest {
}
private void runFlow() throws Exception {
SessionMetadata sessionMetadata = new TestSessionMetadata();
SessionMetadata sessionMetadata = new HttpSessionMetadata(new FakeHttpSession());
sessionMetadata.setClientId("TheRegistrar");
new FlowRunner(
getFlowClass(eppLoader.getEpp()),

View file

@ -0,0 +1,124 @@
// Copyright 2016 The Domain Registry 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.testing;
import static com.google.common.base.Preconditions.checkState;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;
/** A fake {@link HttpSession} that only provides support for getting/setting attributes. */
@SuppressWarnings("deprecation")
public class FakeHttpSession implements HttpSession {
private final Map<String, Object> map = new HashMap<>();
boolean isValid = true;
@Override
public long getCreationTime() {
throw new UnsupportedOperationException();
}
@Override
public String getId() {
throw new UnsupportedOperationException();
}
@Override
public long getLastAccessedTime() {
throw new UnsupportedOperationException();
}
@Override
public ServletContext getServletContext() {
throw new UnsupportedOperationException();
}
@Override
public void setMaxInactiveInterval(int interval) {
throw new UnsupportedOperationException();
}
@Override
public int getMaxInactiveInterval() {
throw new UnsupportedOperationException();
}
@Override
public HttpSessionContext getSessionContext() {
throw new UnsupportedOperationException();
}
@Override
public Object getAttribute(@Nullable String name) {
checkState(isValid, "This session has been invalidated.");
return map.get(name);
}
@Override
public Object getValue(@Nullable String name) {
throw new UnsupportedOperationException();
}
@Override
public Enumeration<?> getAttributeNames() {
throw new UnsupportedOperationException();
}
@Override
public String[] getValueNames() {
throw new UnsupportedOperationException();
}
@Override
public void setAttribute(@Nullable String name, @Nullable Object value) {
checkState(isValid, "This session has been invalidated.");
map.put(name, value);
}
@Override
public void putValue(@Nullable String name, @Nullable Object value) {
throw new UnsupportedOperationException();
}
@Override
public void removeAttribute(@Nullable String name) {
checkState(isValid, "This session has been invalidated.");
map.remove(name);
}
@Override
public void removeValue(@Nullable String name) {
throw new UnsupportedOperationException();
}
@Override
public void invalidate() {
isValid = false;
map.clear();
}
@Override
public boolean isNew() {
throw new UnsupportedOperationException();
}
}

View file

@ -1,52 +0,0 @@
// Copyright 2016 The Domain Registry 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.testing;
import static com.google.common.base.Preconditions.checkState;
import google.registry.flows.SessionMetadata;
import java.util.HashMap;
import java.util.Map;
public class TestSessionMetadata extends SessionMetadata {
private final Map<String, Object> properties = new HashMap<>();
private boolean isValid = true;
@Override
protected void setProperty(String key, Object value) {
properties.put(key, value);
}
@Override
protected Object getProperty(String key) {
return properties.get(key);
}
@Override
public void checkValid() {
checkState(isValid, "Session was invalidated");
}
@Override
public void invalidate() {
isValid = false;
}
public boolean isValid() {
return isValid;
}
}