package sonia.scm.api.v2.resources;

import com.github.sdorra.shiro.ShiroRule;
import com.github.sdorra.shiro.SubjectAware;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.mock.MockDispatcherFactory;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.security.AccessToken;
import sonia.scm.security.AccessTokenBuilder;
import sonia.scm.security.AccessTokenBuilderFactory;
import sonia.scm.security.AccessTokenCookieIssuer;
import sonia.scm.security.DefaultAccessTokenCookieIssuer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.Optional;

import static java.net.URI.create;
import static java.util.Optional.empty;
import static java.util.Optional.of;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@SubjectAware(
  configuration = "classpath:sonia/scm/repository/shiro.ini"
)
@RunWith(MockitoJUnitRunner.class)
public class AuthenticationResourceTest {

  @Rule
  public ShiroRule shiro = new ShiroRule();

  private Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();

  @Mock
  private AccessTokenBuilderFactory accessTokenBuilderFactory;

  @Mock
  private AccessTokenBuilder accessTokenBuilder;

  private AccessTokenCookieIssuer cookieIssuer = new DefaultAccessTokenCookieIssuer(mock(ScmConfiguration.class));

  private MockHttpResponse response = new MockHttpResponse();

  private static final String AUTH_JSON_TRILLIAN = "{\n" +
    "\t\"cookie\": true,\n" +
    "\t\"grant_type\": \"password\",\n" +
    "\t\"username\": \"trillian\",\n" +
    "\t\"password\": \"secret\"\n" +
    "}";

  private static final String AUTH_FORMENCODED_TRILLIAN = "cookie=true&grant_type=password&username=trillian&password=secret";

  private static final String AUTH_JSON_TRILLIAN_WRONG_PW = "{\n" +
    "\t\"cookie\": true,\n" +
    "\t\"grant_type\": \"password\",\n" +
    "\t\"username\": \"trillian\",\n" +
    "\t\"password\": \"justWrong\"\n" +
    "}";

  private static final String AUTH_JSON_NOT_EXISTING_USER = "{\n" +
    "\t\"cookie\": true,\n" +
    "\t\"grant_type\": \"password\",\n" +
    "\t\"username\": \"iDoNotExist\",\n" +
    "\t\"password\": \"doesNotMatter\"\n" +
    "}";

  private static final String AUTH_JSON_WITHOUT_USERNAME = String.join("\n",
    "{",
    "\"grant_type\": \"password\",",
    "\"password\": \"tricia123\"",
    "}"
  );

  private static final String AUTH_JSON_WITHOUT_PASSWORD = String.join("\n",
    "{",
    "\"grant_type\": \"password\",",
    "\"username\": \"trillian\"",
    "}"
  );

  private static final String AUTH_JSON_WITHOUT_GRANT_TYPE = String.join("\n",
    "{",
    "\"username\": \"trillian\",",
    "\"password\": \"tricia123\"",
    "}"
  );

  private static final String AUTH_JSON_WITH_INVALID_GRANT_TYPE = String.join("\n",
    "{",
    "\"grant_type\": \"el speciale\",",
    "\"username\": \"trillian\",",
    "\"password\": \"tricia123\"",
    "}"
  );

  private AuthenticationResource authenticationResource;

  @Before
  public void prepareEnvironment() {
    authenticationResource = new AuthenticationResource(accessTokenBuilderFactory, cookieIssuer);
    dispatcher.getRegistry().addSingletonResource(authenticationResource);

    AccessToken accessToken = mock(AccessToken.class);
    when(accessToken.getExpiration()).thenReturn(new Date(Long.MAX_VALUE));
    when(accessTokenBuilder.build()).thenReturn(accessToken);

    when(accessTokenBuilderFactory.create()).thenReturn(accessTokenBuilder);

    HttpServletRequest servletRequest = mock(HttpServletRequest.class);
    ResteasyProviderFactory.getContextDataMap().put(HttpServletRequest.class, servletRequest);

    HttpServletResponse servletResponse = mock(HttpServletResponse.class);
    ResteasyProviderFactory.getContextDataMap().put(HttpServletResponse.class, servletResponse);
  }

  @Test
  public void shouldAuthCorrectly() throws URISyntaxException {

    MockHttpRequest request = getMockHttpRequest(AUTH_JSON_TRILLIAN);

    dispatcher.invoke(request, response);

    assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
  }

  @Test
  public void shouldAuthCorrectlyWithFormencodedData() throws URISyntaxException {

    MockHttpRequest request = getMockHttpRequestUrlEncoded(AUTH_FORMENCODED_TRILLIAN);

    dispatcher.invoke(request, response);

    assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
  }


  @Test
  public void shouldNotAuthUserWithWrongPassword() throws URISyntaxException {

    MockHttpRequest request = getMockHttpRequest(AUTH_JSON_TRILLIAN_WRONG_PW);

    dispatcher.invoke(request, response);

    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
  }

  @Test
  public void shouldNotAuthNonexistingUser() throws URISyntaxException {
    MockHttpRequest request = getMockHttpRequest(AUTH_JSON_NOT_EXISTING_USER);

    dispatcher.invoke(request, response);

    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
  }

  @Test
  public void shouldReturnBadStatusIfPasswordParameterIsMissing() throws URISyntaxException {
    shouldReturnBadRequest(AUTH_JSON_WITHOUT_USERNAME);
  }

  @Test
  public void shouldReturnBadStatusIfUsernameParameterIsMissing() throws URISyntaxException {
    shouldReturnBadRequest(AUTH_JSON_WITHOUT_PASSWORD);
  }

  @Test
  public void shouldReturnBadStatusIfGrantTypeParameterIsMissing() throws URISyntaxException {
    shouldReturnBadRequest(AUTH_JSON_WITHOUT_GRANT_TYPE);
  }

  @Test
  public void shouldReturnBadStatusIfGrantTypeParameterIsInvalid() throws URISyntaxException {
    shouldReturnBadRequest(AUTH_JSON_WITH_INVALID_GRANT_TYPE);
  }

  @Test
  @SubjectAware(username = "trillian", password = "secret")
  public void shouldSuccessfullyLogoutUser() throws URISyntaxException {
    MockHttpRequest request = MockHttpRequest.delete("/" + AuthenticationResource.PATH + "/access_token");

    dispatcher.invoke(request, response);
    assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
  }

  @Test
  public void shouldHandleLogoutRedirection() throws URISyntaxException, UnsupportedEncodingException {
    authenticationResource.setLogoutRedirection(() -> of(create("http://example.com/cas/logout")));

    MockHttpRequest request = MockHttpRequest.delete("/" + AuthenticationResource.PATH + "/access_token");

    dispatcher.invoke(request, response);

    assertEquals(HttpServletResponse.SC_OK, response.getStatus());
    assertThat(response.getContentAsString(), containsString("http://example.com/cas/logout"));
  }

  @Test
  public void shouldHandleDisabledLogoutRedirection() throws URISyntaxException {
    authenticationResource.setLogoutRedirection(Optional::empty);

    MockHttpRequest request = MockHttpRequest.delete("/" + AuthenticationResource.PATH + "/access_token");

    dispatcher.invoke(request, response);

    assertEquals(HttpServletResponse.SC_NO_CONTENT, response.getStatus());
  }

  private void shouldReturnBadRequest(String requestBody) throws URISyntaxException {
    MockHttpRequest request = getMockHttpRequest(requestBody);

    dispatcher.invoke(request, response);

    assertEquals(HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
  }

  private MockHttpRequest getMockHttpRequest(String jsonPayload) throws URISyntaxException {
    MockHttpRequest request = MockHttpRequest.post("/" + AuthenticationResource.PATH + "/access_token");

    request.content(jsonPayload.getBytes());
    request.contentType(MediaType.APPLICATION_JSON_TYPE);
    return request;
  }

  private MockHttpRequest getMockHttpRequestUrlEncoded(String payload) throws URISyntaxException {
    MockHttpRequest request = MockHttpRequest.post("/" + AuthenticationResource.PATH + "/access_token");

    request.content(payload.getBytes());
    request.contentType(MediaType.APPLICATION_FORM_URLENCODED_TYPE);
    return request;
  }
}
