summaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/restapi/user/UserApiOnPremTest.java
blob: acd481030e26a1e2ed88169826eb9e7049a3e11e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.user;

import com.yahoo.application.container.handler.Request;
import com.yahoo.vespa.athenz.api.AthenzDomain;
import com.yahoo.vespa.athenz.api.AthenzIdentity;
import com.yahoo.vespa.athenz.utils.AthenzIdentities;
import com.yahoo.vespa.hosted.controller.ControllerTester;
import com.yahoo.vespa.hosted.controller.api.integration.user.User;
import com.yahoo.vespa.hosted.controller.restapi.ContainerTester;
import com.yahoo.vespa.hosted.controller.restapi.ControllerContainerTest;
import org.junit.Test;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @author freva
 */
public class UserApiOnPremTest extends ControllerContainerTest {

    private static final String responseFiles = "src/test/java/com/yahoo/vespa/hosted/controller/restapi/user/responses/";

    @Test
    public void userMetadataOnPremTest() {
        ContainerTester tester = new ContainerTester(container, responseFiles);
        ControllerTester controller = new ControllerTester(tester);
        User user = new User("dev@domail", "Joe Developer", "dev", null);

        controller.createTenant("tenant1", "domain1", 1L);
        controller.createApplication("tenant1", "app1", "default");
        controller.createApplication("tenant1", "app2", "default");
        controller.createApplication("tenant1", "app2", "myinstance");
        controller.createApplication("tenant1", "app3");

        controller.createTenant("tenant2", "domain2", 2L);
        controller.createApplication("tenant2", "app2", "test");

        controller.createTenant("tenant3", "domain3", 3L);
        controller.createApplication("tenant3", "app1");

        controller.createTenant("sandbox", "domain4", 4L);
        controller.createApplication("sandbox", "app1", "default");
        controller.createApplication("sandbox", "app2", "default");
        controller.createApplication("sandbox", "app2", "dev");

        AthenzIdentity operator = AthenzIdentities.from("vespa.alice");
        controller.athenzDb().addHostedOperator(operator);
        AthenzIdentity tenantAdmin = AthenzIdentities.from("domain1.bob");
        Stream.of("domain1", "domain2", "domain4")
                .map(AthenzDomain::new)
                .map(controller.athenzDb()::getOrCreateDomain)
                .forEach(d -> d.admin(AthenzIdentities.from("domain1.bob")));

        tester.assertResponse(createUserRequest(user, operator),
                new File("user-without-applications.json"));

        tester.assertResponse(createUserRequest(user, tenantAdmin),
                new File("user-with-applications-athenz.json"));
    }

    private Request createUserRequest(User user, AthenzIdentity identity) {
        Request request = new Request("http://localhost:8080/user/v1/user");
        Map<String, String> userAttributes = new HashMap<>();
        userAttributes.put("email", user.email());
        if (user.name() != null)
            userAttributes.put("name", user.name());
        if (user.nickname() != null)
            userAttributes.put("nickname", user.nickname());
        if (user.picture() != null)
            userAttributes.put("picture", user.picture());
        request.getAttributes().put(User.ATTRIBUTE_NAME, Map.copyOf(userAttributes));
        return addIdentityToRequest(request, identity);
    }
}