Result result = callAction(
controllers.routes.ref.LoginController.authenticate(),
fakeRequest().withFormUrlEncodedBody(TestUtils.SUCCESSFUL_LOGIN_MAP)
);
Context.current.set(fakeContext(Json.toJson(
new Object() {
public String foo = "goodUser";
public String password = "goodPassword";
}).toString()));
result = auth.authenticate();
assertThat(status(result)).isEqualTo(Status.BAD_REQUEST);
public static Context fakeContext(String body)
{
Request request = mock(Request.class);
RequestBody requestBody = mock(RequestBody.class);
when( request.body() ).thenReturn(requestBody);
if(StringUtils.isNotEmpty(body))
{
when( request.method() ).thenReturn("POST");
when( requestBody.asText() ).thenReturn(body);
try
{
when( requestBody.asJson() ).thenReturn(new ObjectMapper().readTree(body));
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
when(request.host()).thenReturn("localhost");
RequestHeader header = mock(RequestHeader.class);
when( header.host()).thenReturn("localhost");
Context ctx = mock(Context.class);
when(ctx._requestHeader()).thenReturn(header);
when(ctx.request()).thenReturn(request);
when(ctx.flash()).thenReturn(new Flash(new HashMap<String,String>()));
when(ctx.lang()).thenReturn(Lang.forCode("en"));
return ctx;
}