package controllers;

import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
import play.mvc.Result;
import play.test.WithApplication;

import static org.fest.assertions.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static play.test.Helpers.callAction;
import static play.test.Helpers.fakeApplication;
import static play.test.Helpers.fakeRequest;
import static play.test.Helpers.flash;
import static play.test.Helpers.session;
import static play.test.Helpers.status;


public class AuthTest extends WithApplication {

    @Before
    public void setUp() {
        start(fakeApplication());
    }

    @Test
    public void authenticateSuccess() {
        Result result = callAction(
            controllers.routes.ref.Application.authenticate(),
            fakeRequest().withFormUrlEncodedBody(ImmutableMap.of(
                "email", "foo@bar.com",
                "password", "password")));
        assertEquals(status(result), 303);
        assertEquals(session(result).get("email"), "foo@bar.com");
    }

    @Test
    public void authenticateFailure() {
        Result result = callAction(
            controllers.routes.ref.Application.authenticate(),
            fakeRequest().withFormUrlEncodedBody(ImmutableMap.of(
                "email", "",
                "password", "")));
        assertEquals(status(result), 400);
        assertNull(session(result).get("email"));
    }

    @Test
    public void logoutSuccess() {
        Result result = callAction(
            controllers.routes.ref.Application.logout(),
            fakeRequest().withSession("email", "foo@bar.com"));
        assertEquals(status(result), 303);
        assertNull(session(result).get("email"));
        assertThat(flash(result).get("success")).isEqualTo(
            "Alright. See you around.");
    }

}

@@ -491,16 +491,56 @@
 .flash;%0A
+import static play.test.Helpers.header;%0A
 import s
@@ -1938,10 +1938,2740 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void authenticated() %7B%0A        Result rulesResult = callAction(%0A            controllers.routes.ref.Application.rules(),%0A            fakeRequest().withSession(%22email%22, %22foo@bar.com%22));%0A        assertEquals(status(rulesResult), 200);%0A        Result ruleResult = callAction(%0A            controllers.routes.ref.Application.rule(1),%0A            fakeRequest().withSession(%22email%22, %22foo@bar.com%22));%0A        assertEquals(status(ruleResult), 200);%0A        Result searchResult = callAction(%0A            controllers.routes.ref.Application.search(),%0A            fakeRequest().withSession(%22email%22, %22foo@bar.com%22));%0A        assertEquals(status(searchResult), 200);%0A        Result featuresResult = callAction(%0A            controllers.routes.ref.Application.features(),%0A            fakeRequest().withSession(%22email%22, %22foo@bar.com%22));%0A        assertEquals(status(featuresResult), 200);%0A        Result logoutResult = callAction(%0A            controllers.routes.ref.Application.logout(),%0A            fakeRequest().withSession(%22email%22, %22foo@bar.com%22));%0A        assertEquals(status(logoutResult), 303);%0A        assertEquals(%0A            header(%22Location%22, logoutResult),%0A            routes.Application.login().url());%0A    %7D%0A%0A    @Test%0A    public void notAuthenticated() %7B%0A        Result rulesResult = callAction(%0A            controllers.routes.ref.Application.rules(),%0A            fakeRequest());%0A        assertEquals(status(rulesResult), 303);%0A        assertEquals(%0A            header(%22Location%22, rulesResult),%0A            routes.Application.login().url());%0A        Result ruleResult = callAction(%0A            controllers.routes.ref.Application.rule(1),%0A            fakeRequest());%0A        assertEquals(status(ruleResult), 303);%0A        assertEquals(%0A            header(%22Location%22, ruleResult),%0A            routes.Application.login().url());%0A        Result searchResult = callAction(%0A            controllers.routes.ref.Application.search(),%0A            fakeRequest());%0A        assertEquals(status(searchResult), 303);%0A        assertEquals(%0A            header(%22Location%22, searchResult),%0A            routes.Application.login().url());%0A        Result featuresResult = callAction(%0A            controllers.routes.ref.Application.features(),%0A            fakeRequest());%0A        assertEquals(status(featuresResult), 303);%0A        assertEquals(%0A            header(%22Location%22, featuresResult),%0A            routes.Application.login().url());%0A        Result logoutResult = callAction(%0A            controllers.routes.ref.Application.logout(),%0A            fakeRequest());%0A        assertEquals(status(logoutResult), 303);%0A        assertEquals(%0A            header(%22Location%22, logoutResult),%0A            routes.Application.login().url());%0A    %7D%0A%0A
 %7D%0A

Implement high-level tests for `controllers.Secured` authenticator.