-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More firebase tests #374
Merged
Merged
More firebase tests #374
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
.../firebase-tictactoe/src/test/java/com/example/appengine/firetactoe/DeleteServletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Copyright 2016 Google Inc. 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 com.example.appengine.firetactoe; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.fail; | ||
import static org.mockito.Mockito.eq; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.api.client.http.LowLevelHttpRequest; | ||
import com.google.api.client.http.LowLevelHttpResponse; | ||
import com.google.api.client.testing.http.MockHttpTransport; | ||
import com.google.api.client.testing.http.MockLowLevelHttpRequest; | ||
import com.google.api.client.testing.http.MockLowLevelHttpResponse; | ||
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; | ||
import com.google.appengine.tools.development.testing.LocalServiceTestHelper; | ||
import com.google.appengine.tools.development.testing.LocalURLFetchServiceTestConfig; | ||
import com.google.appengine.tools.development.testing.LocalUserServiceTestConfig; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.googlecode.objectify.Objectify; | ||
import com.googlecode.objectify.ObjectifyFactory; | ||
import com.googlecode.objectify.ObjectifyService; | ||
import com.googlecode.objectify.util.Closeable; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.mockito.Matchers; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* Unit tests for {@link DeleteServlet}. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class DeleteServletTest { | ||
private static final String USER_EMAIL = "[email protected]"; | ||
private static final String USER_ID = "whiskytangofoxtrot"; | ||
private static final String FIREBASE_DB_URL = "http://firebase.com/dburl"; | ||
|
||
private final LocalServiceTestHelper helper = | ||
new LocalServiceTestHelper( | ||
// Set no eventual consistency, that way queries return all results. | ||
// http://g.co/cloud/appengine/docs/java/tools/localunittesting#Java_Writing_High_Replication_Datastore_tests | ||
new LocalDatastoreServiceTestConfig().setDefaultHighRepJobPolicyUnappliedJobPercentage(0), | ||
new LocalUserServiceTestConfig(), | ||
new LocalURLFetchServiceTestConfig() | ||
) | ||
.setEnvEmail(USER_EMAIL) | ||
.setEnvAuthDomain("gmail.com") | ||
.setEnvAttributes(new HashMap( | ||
ImmutableMap.of("com.google.appengine.api.users.UserService.user_id_key", USER_ID))); | ||
|
||
@Mock private HttpServletRequest mockRequest; | ||
@Mock private HttpServletResponse mockResponse; | ||
protected Closeable dbSession; | ||
|
||
private DeleteServlet servletUnderTest; | ||
|
||
@BeforeClass | ||
public static void setUpBeforeClass() { | ||
// Reset the Factory so that all translators work properly. | ||
ObjectifyService.setFactory(new ObjectifyFactory()); | ||
ObjectifyService.register(Game.class); | ||
// Mock out the firebase config | ||
FirebaseChannel.firebaseConfigStream = new ByteArrayInputStream( | ||
String.format("databaseURL: \"%s\"", FIREBASE_DB_URL).getBytes()); | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
helper.setUp(); | ||
dbSession = ObjectifyService.begin(); | ||
|
||
servletUnderTest = new DeleteServlet(); | ||
|
||
helper.setEnvIsLoggedIn(true); | ||
// Make sure there are no firebase requests if we don't expect it | ||
FirebaseChannel.getInstance().httpTransport = null; | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
dbSession.close(); | ||
helper.tearDown(); | ||
} | ||
|
||
@Test | ||
public void doPost_noGameKey() throws Exception { | ||
try { | ||
servletUnderTest.doPost(mockRequest, mockResponse); | ||
fail("Should not succeed with no gameKey specified."); | ||
} catch (IllegalArgumentException e) { | ||
assertThat(e.getMessage()).startsWith("id 'null'"); | ||
} | ||
} | ||
|
||
@Test | ||
public void doPost_deleteGame() throws Exception { | ||
// Insert a game | ||
Objectify ofy = ObjectifyService.ofy(); | ||
Game game = new Game(USER_ID, "my-opponent", " ", true); | ||
ofy.save().entity(game).now(); | ||
String gameKey = game.getId(); | ||
when(mockRequest.getParameter("gameKey")).thenReturn(gameKey); | ||
|
||
// Mock out the firebase response. See | ||
// http://g.co/dv/api-client-library/java/google-http-java-client/unit-testing | ||
MockHttpTransport mockHttpTransport = spy(new MockHttpTransport() { | ||
@Override | ||
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException { | ||
return new MockLowLevelHttpRequest() { | ||
@Override | ||
public LowLevelHttpResponse execute() throws IOException { | ||
MockLowLevelHttpResponse response = new MockLowLevelHttpResponse(); | ||
response.setStatusCode(200); | ||
return response; | ||
} | ||
}; | ||
} | ||
}); | ||
FirebaseChannel.getInstance().httpTransport = mockHttpTransport; | ||
|
||
servletUnderTest.doPost(mockRequest, mockResponse); | ||
|
||
verify(mockHttpTransport, times(1)).buildRequest( | ||
eq("DELETE"), Matchers.matches(FIREBASE_DB_URL + "/channels/[\\w-]+.json$")); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can specify
https
only in yourappengine-web.xml
. I don't think this will require being logged in, if that's what your trying for.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm just trying to require sign-in, as per this article
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep - doesn't work that way in flex. At least not easily.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, but you are in standard. That article suggests that you block subpaths /auth/* Not /*
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right - the tictactoe app requires every path to be authenticated, so I modified it to block all paths.