String sFakeUser = USER_TEST + UUID.randomUUID();
// Prepare test user
JsonNode node = updatePayloadFieldValue("/adminUserCreatePayload.json", "username", sFakeUser);
// Create user
FakeRequest request = new FakeRequest(getMethod(), getRouteAddress());
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withJsonBody(node, getMethod());
Result result = routeAndCall(request);
assertRoute(result, "routeCreateUser_2", Status.CREATED, null, false);
String body = play.test.Helpers.contentAsString(result);
JsonNode jsonRes = Json.parse(body);
String token = jsonRes.get("data").get(SessionKeys.TOKEN.toString()).textValue();
//test if the user can execute a GET on himself
request = new FakeRequest(GET, getRouteAddress());
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_TOKEN, token);
result = route(request);
assertRoute(result, "routeCreateUser_check", Status.OK, null, false);
//now we suspend it
request = new FakeRequest(PUT, getRouteAddress()+"/suspend");
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_TOKEN, token);
result = route(request);
assertRoute(result, "routeCreateUser_suspend", Status.OK, null, false);
//test if the user can execute a GET on himself
request = new FakeRequest(GET, getRouteAddress());
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_TOKEN, token);
result = route(request);
assertRoute(result, "routeCreateUser_check_2", Status.UNAUTHORIZED, null, false);
//now the admin reactivate him
request = new FakeRequest(PUT, "/admin/user/activate/" + sFakeUser);
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(TestConfig.AUTH_ADMIN));
result = route(request);
assertRoute(result, "routeCreateUser_activate", Status.OK, null, false);
//the user should can connect again
request = new FakeRequest(GET, getRouteAddress());
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_TOKEN, token);
result = route(request);
assertRoute(result, "routeCreateUser_check_3", Status.OK, null, false);
//now the admin suspend him again
request = new FakeRequest(PUT, "/admin/user/suspend/" + sFakeUser);
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_AUTH, TestConfig.encodeAuth(TestConfig.AUTH_ADMIN));
result = route(request);
assertRoute(result, "routeCreateUser_admin_suspend", Status.OK, null, false);
//and the user cannot login
request = new FakeRequest(GET, getRouteAddress());
request = request.withHeader(TestConfig.KEY_APPCODE, TestConfig.VALUE_APPCODE);
request = request.withHeader(TestConfig.KEY_TOKEN, token);
result = route(request);
assertRoute(result, "routeCreateUser_check_4", Status.UNAUTHORIZED, null, false);
}
}
);