Search in sources :

Example 1 with YamlCredentials

use of com.artipie.front.auth.YamlCredentials in project front by artipie.

the class GetUserTest method returnsUserInfo.

@ParameterizedTest
@CsvSource(value = { "Alice;{\"Alice\":{\"email\":\"alice@example.com\",\"groups\":[]}}", "John;{\"John\":{\"groups\":[\"reader\",\"dev-lead\"]}}", "Mark;{\"Mark\":{\"email\":\"Mark@example.com\",\"groups\":[\"admin\",\"writer\"]}}" }, delimiterString = ";")
void returnsUserInfo(final String name, final String res) throws JSONException {
    final var rqs = Mockito.mock(Request.class);
    Mockito.when(rqs.params(GetUser.USER_PARAM.toString())).thenReturn(name);
    JSONAssert.assertEquals(new GetUser(new YamlCredentials(YamlCredentialsTest.credYaml(YamlCredentialsTest.PasswordFormat.SIMPLE, // @checkstyle LineLengthCheck (5 lines)
    new YamlCredentialsTest.User("Alice", Optional.of("alice@example.com")), new YamlCredentialsTest.User("John", Optional.empty(), "reader", "dev-lead"), new YamlCredentialsTest.User("Mark", Optional.of("Mark@example.com"), "writer", "admin")))).handle(rqs, Mockito.mock(Response.class)), res, true);
}
Also used : YamlCredentialsTest(com.artipie.front.auth.YamlCredentialsTest) YamlCredentials(com.artipie.front.auth.YamlCredentials) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with YamlCredentials

use of com.artipie.front.auth.YamlCredentials in project front by artipie.

the class GetUserTest method returnNotFound.

@Test
void returnNotFound() {
    final var rqs = Mockito.mock(Request.class);
    Mockito.when(rqs.params(GetUser.USER_PARAM.toString())).thenReturn("any");
    final Response resp = Mockito.mock(Response.class);
    new GetUser(new YamlCredentials(Yaml.createYamlMappingBuilder().add("credentials", Yaml.createYamlMappingBuilder().build()).build())).handle(rqs, resp);
    Mockito.verify(resp).status(HttpStatus.NOT_FOUND_404);
}
Also used : Response(spark.Response) YamlCredentials(com.artipie.front.auth.YamlCredentials) YamlCredentialsTest(com.artipie.front.auth.YamlCredentialsTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with YamlCredentials

use of com.artipie.front.auth.YamlCredentials in project front by artipie.

the class HeadUserTest method returnsOkWhenUserFound.

@Test
void returnsOkWhenUserFound() {
    final var rqs = Mockito.mock(Request.class);
    final String uid = "Alice";
    Mockito.when(rqs.params(GetUser.USER_PARAM.toString())).thenReturn(uid);
    final Response resp = Mockito.mock(Response.class);
    new HeadUser(new YamlCredentials(YamlCredentialsTest.credYaml(YamlCredentialsTest.PasswordFormat.SIMPLE, new YamlCredentialsTest.User(uid, "plain", "123")))).handle(rqs, resp);
    Mockito.verify(resp).status(HttpStatus.OK_200);
}
Also used : Response(spark.Response) YamlCredentials(com.artipie.front.auth.YamlCredentials) Test(org.junit.jupiter.api.Test) YamlCredentialsTest(com.artipie.front.auth.YamlCredentialsTest)

Example 4 with YamlCredentials

use of com.artipie.front.auth.YamlCredentials in project front by artipie.

the class HeadUserTest method returnsNotFoundWhenUserDoesNotExists.

@Test
void returnsNotFoundWhenUserDoesNotExists() {
    final var rqs = Mockito.mock(Request.class);
    Mockito.when(rqs.params(GetUser.USER_PARAM.toString())).thenReturn("Someone");
    final Response resp = Mockito.mock(Response.class);
    new HeadUser(new YamlCredentials(YamlCredentialsTest.credYaml(YamlCredentialsTest.PasswordFormat.SIMPLE, new YamlCredentialsTest.User("John", "plain", "123")))).handle(rqs, resp);
    Mockito.verify(resp).status(HttpStatus.NOT_FOUND_404);
}
Also used : Response(spark.Response) YamlCredentials(com.artipie.front.auth.YamlCredentials) Test(org.junit.jupiter.api.Test) YamlCredentialsTest(com.artipie.front.auth.YamlCredentialsTest)

Example 5 with YamlCredentials

use of com.artipie.front.auth.YamlCredentials in project front by artipie.

the class ArtipieYaml method credentials.

/**
 * Credentials from config.
 * @return Credentials
 */
public Credentials credentials() {
    final Optional<Key> key = this.fileCredentialsKey();
    Optional<YamlMapping> res = Optional.empty();
    if (key.isPresent() && this.storage().exists(key.get())) {
        try {
            res = Optional.of(Yaml.createYamlInput(new String(this.storage().value(key.get()), StandardCharsets.UTF_8)).readYamlMapping());
        } catch (final IOException err) {
            throw new UncheckedIOException(err);
        }
    } else if (key.isPresent()) {
        res = Optional.of(Yaml.createYamlMappingBuilder().build());
    }
    return new YamlCredentials(res.orElseThrow(() -> new NotImplementedException("Not implemented yet")));
}
Also used : NotImplementedException(org.apache.commons.lang3.NotImplementedException) UncheckedIOException(java.io.UncheckedIOException) YamlCredentials(com.artipie.front.auth.YamlCredentials) YamlMapping(com.amihaiemil.eoyaml.YamlMapping) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Key(com.artipie.asto.Key)

Aggregations

YamlCredentials (com.artipie.front.auth.YamlCredentials)5 YamlCredentialsTest (com.artipie.front.auth.YamlCredentialsTest)4 Test (org.junit.jupiter.api.Test)3 Response (spark.Response)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 YamlMapping (com.amihaiemil.eoyaml.YamlMapping)1 Key (com.artipie.asto.Key)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 NotImplementedException (org.apache.commons.lang3.NotImplementedException)1 CsvSource (org.junit.jupiter.params.provider.CsvSource)1