package oasis.web.authn;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;

import java.net.URI;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.jukito.JukitoModule;
import org.jukito.JukitoRunner;
import org.jukito.TestSingleton;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.inject.Inject;

import oasis.http.testing.InProcessResteasy;
import oasis.model.authn.SidToken;
import oasis.model.authn.TokenRepository;
import oasis.services.authn.TokenHandler;

@RunWith(JukitoRunner.class)
public class LogoutPageTest {

  public static class Module extends JukitoModule {
    @Override
    protected void configureTest() {
      bind(LogoutPage.class);

      bindMock(TokenHandler.class).in(TestSingleton.class);

    }
  }

  private static final SidToken sidToken = new SidToken() {{
    setId("sessionId");
  }};

  @Inject @Rule public InProcessResteasy resteasy;

  @Before public void setUpMocks(TokenHandler tokenHandler) {
    when(tokenHandler.getCheckedToken("sid", SidToken.class)).thenReturn(sidToken);
  }

  @Before public void setUp() {
    resteasy.getDeployment().getRegistry().addPerRequestResource(LogoutPage.class);
    resteasy.getDeployment().getProviderFactory().register(UserFilter.class);
  }

  @Test public void testLegacyGet_loggedIn_noContinueUrl(TokenRepository tokenRepository) {
    Response response = resteasy.getClient().target(UriBuilder.fromResource(LogoutPage.class)).request()
        .cookie(UserFilter.COOKIE_NAME, "sid")
        .get();

    assertThat(response.getStatusInfo()).isEqualTo(Response.Status.SEE_OTHER);
    assertThat(response.getLocation()).isEqualTo(LoginPage.defaultContinueUrl(UriBuilder.fromUri(InProcessResteasy.BASE_URI)));
    assertThat(response.getCookies()).containsKey(UserFilter.COOKIE_NAME);
    assertThat(response.getCookies().get(UserFilter.COOKIE_NAME).getExpiry()).isInThePast();

    verify(tokenRepository).revokeToken(sidToken.getId());
  }

  @Test public void testLegacyGet_loggedIn_withContinueUrl(TokenRepository tokenRepository) {
    Response response = resteasy.getClient().target(UriBuilder.fromResource(LogoutPage.class))
        .queryParam(LoginPage.CONTINUE_PARAM, "http://www.google.com")
        .request()
        .cookie(UserFilter.COOKIE_NAME, "sid")
        .get();

    assertThat(response.getStatusInfo()).isEqualTo(Response.Status.SEE_OTHER);
    assertThat(response.getLocation()).isEqualTo(URI.create("http://www.google.com"));
    assertThat(response.getCookies()).containsKey(UserFilter.COOKIE_NAME);
    assertThat(response.getCookies().get(UserFilter.COOKIE_NAME).getExpiry()).isInThePast();

    verify(tokenRepository).revokeToken(sidToken.getId());
  }

  @Test public void testLegacyGet_notLoggedIn_noContinueUrl() {
    Response response = resteasy.getClient().target(UriBuilder.fromResource(LogoutPage.class)).request().get();

    assertThat(response.getStatusInfo()).isEqualTo(Response.Status.SEE_OTHER);
    assertThat(response.getLocation()).isEqualTo(LoginPage.defaultContinueUrl(UriBuilder.fromUri(InProcessResteasy.BASE_URI)));
    if (response.getCookies().containsKey(UserFilter.COOKIE_NAME)) {
      assertThat(response.getCookies().get(UserFilter.COOKIE_NAME).getExpiry()).isInThePast();
    }
  }

  @Test public void testLegacyGet_notLoggedIn_withContinueUrl() {
    Response response = resteasy.getClient().target(UriBuilder.fromResource(LogoutPage.class))
        .queryParam(LoginPage.CONTINUE_PARAM, "http://www.google.com")
        .request()
        .get();

    assertThat(response.getStatusInfo()).isEqualTo(Response.Status.SEE_OTHER);
    assertThat(response.getLocation()).isEqualTo(URI.create("http://www.google.com"));
    if (response.getCookies().containsKey(UserFilter.COOKIE_NAME)) {
      assertThat(response.getCookies().get(UserFilter.COOKIE_NAME).getExpiry()).isInThePast();
    }
  }
}

@@ -267,41 +267,8 @@
 er;%0A
-import org.jukito.TestSingleton;%0A
 impo
@@ -288,16 +288,16 @@
 Before;%0A
+
 import o
@@ -541,33 +541,38 @@
 sis.
-services.authn.TokenHandl
+web.authn.testing.TestUserFilt
 er;%0A
@@ -770,68 +770,8 @@
 );%0A%0A
-      bindMock(TokenHandler.class).in(TestSingleton.class);%0A
 %0A   
@@ -959,120 +959,11 @@
 cks(
-TokenHandler tokenHandler) %7B%0A    when(tokenHandler.getCheckedToken(%22sid%22, SidToken.class)).thenReturn(sidToken);
+) %7B
 %0A  %7D
@@ -1084,86 +1084,8 @@
 s);%0A
-    resteasy.getDeployment().getProviderFactory().register(UserFilter.class);%0A
   %7D%0A
@@ -1169,32 +1169,123 @@
 enRepository) %7B%0A
+    resteasy.getDeployment().getProviderFactory().register(new TestUserFilter(sidToken));%0A%0A
     Response res
@@ -1376,64 +1376,8 @@
 st()
-%0A        .cookie(UserFilter.COOKIE_NAME, %22sid%22)%0A        
 .get
@@ -1911,24 +1911,115 @@
 pository) %7B%0A
+    resteasy.getDeployment().getProviderFactory().register(new TestUserFilter(sidToken));%0A%0A
     Response
@@ -2164,32 +2164,32 @@
 ww.google.com%22)%0A
+
         .request
@@ -2195,55 +2195,8 @@
 t()%0A
-        .cookie(UserFilter.COOKIE_NAME, %22sid%22)%0A
     

Simplify LogoutPageTest to avoid relying on UserFilter