aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-athenz/src/test/java/com/yahoo/vespa/athenz/aws/AwsCredentialsTest.java
blob: bf52d4357c9c83bbd72aa2aca04a12542f91d9c0 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.aws;

import com.yahoo.vespa.athenz.api.AwsTemporaryCredentials;
import org.junit.jupiter.api.Test;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author tokle
 */
public class AwsCredentialsTest {

    @Test
    void refreshes_correctly() {
        Clock clock = Clock.systemUTC();
        // Does not require refresh when expires in 10 minutes
        assertFalse(AwsCredentials.shouldRefresh(getCredentials(clock.instant().plus(Duration.ofMinutes(10)))));

        // Requires refresh when expires in 3 minutes
        assertTrue(AwsCredentials.shouldRefresh(getCredentials(clock.instant().plus(Duration.ofMinutes(3)))));

        // Requires refresh when expired
        assertTrue(AwsCredentials.shouldRefresh(getCredentials(clock.instant().minus(Duration.ofMinutes(1)))));

        // Refreshes when no credentials provided
        assertTrue(AwsCredentials.shouldRefresh(null));
    }

    private AwsTemporaryCredentials getCredentials(Instant expiration) {
        return new AwsTemporaryCredentials("accesskey", "secretaccesskey", "sessionToken", expiration);
    }
}