/src/test/java/io/lavagna/web/security/login/OauthLoginTest.java

https://github.com/digitalfondue/lavagna · Java · 133 lines · 94 code · 19 blank · 20 comment · 0 complexity · 29f8cd2c6ddbde647885bf7758c9d621 MD5 · raw file

  1. /**
  2. * This file is part of lavagna.
  3. *
  4. * lavagna is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * lavagna is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with lavagna. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package io.lavagna.web.security.login;
  18. import io.lavagna.web.security.SecurityConfiguration.SessionHandler;
  19. import io.lavagna.web.security.SecurityConfiguration.Users;
  20. import io.lavagna.web.security.login.OAuthLogin.OAuthConfiguration;
  21. import io.lavagna.web.security.login.OAuthLogin.OauthConfigurationFetcher;
  22. import io.lavagna.web.security.login.oauth.OAuthProvider;
  23. import io.lavagna.web.security.login.oauth.OAuthServiceBuilder;
  24. import org.junit.Assert;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import org.junit.runner.RunWith;
  28. import org.mockito.Mock;
  29. import org.mockito.junit.MockitoJUnitRunner;
  30. import org.scribe.builder.api.Api;
  31. import org.scribe.oauth.OAuthService;
  32. import javax.servlet.http.HttpServletRequest;
  33. import javax.servlet.http.HttpServletResponse;
  34. import javax.servlet.http.HttpSession;
  35. import java.io.IOException;
  36. import java.util.Arrays;
  37. import java.util.List;
  38. import java.util.Map;
  39. import static org.mockito.Matchers.any;
  40. import static org.mockito.Mockito.mock;
  41. import static org.mockito.Mockito.when;
  42. @RunWith(MockitoJUnitRunner.class)
  43. public class OauthLoginTest {
  44. @Mock
  45. private Users users;
  46. @Mock
  47. private SessionHandler sessionHandler;
  48. @Mock
  49. private OauthConfigurationFetcher configurationFetcher;
  50. @Mock
  51. private OAuthServiceBuilder serviceBuilder;
  52. @Mock
  53. private HttpServletResponse resp;
  54. @Mock
  55. private HttpServletRequest req;
  56. @Mock
  57. private HttpSession session;
  58. private OAuthConfiguration configuration;
  59. private String errorPage = "errorPage";
  60. private OAuthLogin oAuthLogin;
  61. @Before
  62. public void prepare() {
  63. configuration = new OAuthConfiguration("http://baseUrl", Arrays.asList(
  64. new OAuthProvider("google", "", ""),
  65. new OAuthProvider("bitbucket", "", "")));
  66. oAuthLogin = new OAuthLogin(users, sessionHandler, configurationFetcher, serviceBuilder, errorPage);
  67. when(configurationFetcher.fetch()).thenReturn(configuration);
  68. when(serviceBuilder.build(any(Api.class), any(String.class), any(String.class), any(String.class), any(String.class))).thenReturn(mock(OAuthService.class));
  69. when(req.getSession()).thenReturn(session);
  70. }
  71. @Test
  72. public void initiateWithoutPost() throws IOException {
  73. when(req.getRequestURI()).thenReturn("/login/oauth/google");
  74. Assert.assertFalse(oAuthLogin.doAction(req, resp));
  75. }
  76. @Test
  77. public void initiateWithPostWrongUrl() throws IOException {
  78. when(req.getRequestURI()).thenReturn("/login/oauth/derp");
  79. when(req.getMethod()).thenReturn("POST");
  80. Assert.assertFalse(oAuthLogin.doAction(req, resp));
  81. }
  82. @Test
  83. public void initiateWithPost() throws IOException {
  84. when(req.getRequestURI()).thenReturn("/login/oauth/google");
  85. when(req.getMethod()).thenReturn("POST");
  86. Assert.assertTrue(oAuthLogin.doAction(req, resp));
  87. //TODO: fixme
  88. //verify(authResultHandler).handleAuthorizationUrl(req, resp);
  89. }
  90. @Test
  91. public void callbackHandle() throws IOException {
  92. when(req.getRequestURI()).thenReturn("/login/oauth/google/callback");
  93. Assert.assertTrue(oAuthLogin.doAction(req, resp));
  94. //TODO: fixme
  95. //verify(authResultHandler).handleCallback(req, resp);
  96. }
  97. @Test
  98. public void callbackHandleForWrongProvider() throws IOException {
  99. when(req.getRequestURI()).thenReturn("/login/oauth/derp/callback");
  100. Assert.assertFalse(oAuthLogin.doAction(req, resp));
  101. }
  102. @Test
  103. public void checkModelForLoginPage() {
  104. when(req.getSession()).thenReturn(mock(HttpSession.class));
  105. Map<String, Object> r = oAuthLogin.modelForLoginPage(req);
  106. @SuppressWarnings("unchecked")
  107. List<String> providers = (List<String>) r.get("loginOauthProviders");
  108. Assert.assertTrue(providers.contains("google"));
  109. Assert.assertTrue(providers.contains("bitbucket"));
  110. Assert.assertFalse(providers.contains("github"));
  111. Assert.assertFalse(providers.contains("twitter"));
  112. Assert.assertTrue(r.containsKey("csrfToken"));
  113. }
  114. }