Search in sources :

Example 6 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class LocalUserFilterTest method test_adds_cookie.

@Test
public void test_adds_cookie() throws Exception {
    LocalUserFilter localUserFilter = new LocalUserFilter(new PropertiesProvider());
    Payload payload = localUserFilter.apply("url", context, nextFilter);
    assertThat(payload.cookies().size()).isEqualTo(1);
    assertThat(payload.cookies().get(0).name()).isEqualTo("_ds_session_id");
    assertThat(payload.cookies().get(0).value()).contains("\"login\":\"local\"");
}
Also used : PropertiesProvider(org.icij.datashare.PropertiesProvider) Payload(net.codestory.http.payload.Payload) Test(org.junit.Test)

Example 7 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class TaskResource method getTaskResult.

/**
 * gets task result with its id
 *
 * @param id
 * @return 200 and the result,
 *         204 if there is no result
 *         404 if the tasks doesn't exist
 *         403 if the task is not belonging to current user
 *
 * Example :
 * $(curl localhost:8080/api/task/21148262/result)
 */
@Get("/:id/result")
public Payload getTaskResult(String id, Context context) {
    TaskView<?> task = forbiddenIfNotSameUser(context, notFoundIfNull(taskManager.get(id)));
    Object result = task.getResult();
    if (result instanceof File) {
        final Path appPath = ((File) result).isAbsolute() ? get(System.getProperty("user.dir")).resolve(context.env().appFolder()) : get(context.env().appFolder());
        Path resultPath = appPath.relativize(((File) result).toPath());
        return new Payload(resultPath).withHeader("Content-Disposition", "attachment;filename=\"" + resultPath.getFileName() + "\"");
    }
    return result == null ? new Payload(204) : new Payload(result);
}
Also used : Path(java.nio.file.Path) Payload(net.codestory.http.payload.Payload) File(java.io.File)

Example 8 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class ApiKeyFilterTest method test_unauthorized_if_type_is_not_bearer.

@Test
public void test_unauthorized_if_type_is_not_bearer() throws Exception {
    when(context.header("authorization")).thenReturn("Basic session_id");
    Payload payload = apiKeyFilter.apply("url", context, nextFilter);
    assertThat(payload.code()).isEqualTo(401);
}
Also used : Payload(net.codestory.http.payload.Payload) Test(org.junit.Test)

Example 9 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class ApiKeyFilterTest method test_does_nothing_if_there_is_datashare_cookie.

@Test
public void test_does_nothing_if_there_is_datashare_cookie() throws Exception {
    when(context.cookies()).thenReturn(new SimpleCookies() {

        {
            put("_ds_session_id", new NewCookie("_ds_session_id", "cookie value"));
        }
    });
    Payload payload = apiKeyFilter.apply("url", context, nextFilter);
    assertThat(payload).isSameAs(next);
}
Also used : Payload(net.codestory.http.payload.Payload) NewCookie(net.codestory.http.NewCookie) Test(org.junit.Test)

Example 10 with Payload

use of net.codestory.http.payload.Payload in project datashare by ICIJ.

the class ApiKeyFilterTest method test_unauthorized_if_no_type.

@Test
public void test_unauthorized_if_no_type() throws Exception {
    when(context.header("authorization")).thenReturn("session_id");
    Payload payload = apiKeyFilter.apply("url", context, nextFilter);
    assertThat(payload.code()).isEqualTo(401);
}
Also used : Payload(net.codestory.http.payload.Payload) Test(org.junit.Test)

Aggregations

Payload (net.codestory.http.payload.Payload)19 Test (org.junit.Test)11 PropertiesProvider (org.icij.datashare.PropertiesProvider)5 HashMap (java.util.HashMap)2 Context (net.codestory.http.Context)2 User (net.codestory.http.security.User)2 BatchSearch (org.icij.datashare.batch.BatchSearch)2 DatashareUser (org.icij.datashare.session.DatashareUser)2 User (org.icij.datashare.user.User)2 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Boolean (java.lang.Boolean)1 Integer.parseInt (java.lang.Integer.parseInt)1 String.format (java.lang.String.format)1 Path (java.nio.file.Path)1 java.util (java.util)1