Skip to content

Commit

Permalink
SK-176 update tests with ctx and ttl changes
Browse files Browse the repository at this point in the history
yaswanth-pula-skyflow committed Nov 29, 2022
1 parent 59d286a commit d0517a6
Showing 2 changed files with 113 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -5,23 +5,41 @@

import com.skyflow.Configuration;
import com.skyflow.common.utils.Helpers;
import com.skyflow.common.utils.HttpUtility;
import com.skyflow.entities.LogLevel;
import com.skyflow.entities.ResponseToken;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.SkyflowException;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;

@RunWith(PowerMockRunner.class)
@PrepareForTest
@PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility"})
public class BearerTokenTest {

private final String VALID_CREDENTIALS_FILE_PATH = "./credentials.json";

@Test
public void testEmptyFilePath() {
Configuration.setLogLevel(LogLevel.DEBUG);
@@ -190,4 +208,68 @@ public void testEmptyRoleProvided() {
}
}

@Test
@PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility"})
public void testValidWithContextString() {
PowerMockito.mockStatic(HttpUtility.class);
String mockResponse = "{\"accessToken\":\"valid.bearer.token\",\"tokenType\":\"Bearer\"}";
try {
PowerMockito.when(HttpUtility.sendRequest(anyString(), ArgumentMatchers.<URL>any(), ArgumentMatchers.<JSONObject>any(), (Map<String, String>) eq(null))).thenReturn(mockResponse);
BearerToken token = new BearerToken.BearerTokenBuilder()
.setCredentials(new File(VALID_CREDENTIALS_FILE_PATH))
.setCtx("abc")
.build();

String bearerToken = token.getBearerToken();
assertEquals(bearerToken,"valid.bearer.token");
} catch (SkyflowException | IOException exception) {
assertNull(exception);
}
}

@Test
@PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility"})
public void testValidWithRoles() {
PowerMockito.mockStatic(HttpUtility.class);
String mockResponse = "{\"accessToken\":\"valid.bearer.token\",\"tokenType\":\"Bearer\"}";
try {
PowerMockito.when(HttpUtility.sendRequest(anyString(), ArgumentMatchers.<URL>any(), ArgumentMatchers.<JSONObject>any(), (Map<String, String>) eq(null))).thenReturn(mockResponse);
BearerToken token = new BearerToken.BearerTokenBuilder()
.setCredentials(new File(VALID_CREDENTIALS_FILE_PATH))
.setCtx("abc")
.setRoles(new String[]{"role_id"})
.build();

String bearerToken = token.getBearerToken();
assertEquals(bearerToken,"valid.bearer.token");
} catch (SkyflowException | IOException exception) {
assertNull(exception);
}
}

@Test
@PrepareForTest(fullyQualifiedNames = {"com.skyflow.common.utils.HttpUtility"})
public void testValidWithContextFromCreds() {
PowerMockito.mockStatic(HttpUtility.class);
String mockResponse = "{\"accessToken\":\"valid.bearer.token\",\"tokenType\":\"Bearer\"}";
try {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(VALID_CREDENTIALS_FILE_PATH));
JSONObject saCreds = (JSONObject) obj;

PowerMockito.when(HttpUtility.sendRequest(anyString(), ArgumentMatchers.<URL>any(), ArgumentMatchers.<JSONObject>any(), (Map<String, String>) eq(null))).thenReturn(mockResponse);
BearerToken token = new BearerToken.BearerTokenBuilder()
.setCredentials(saCreds.toJSONString())
.setCtx("abc")
.setRoles(new String[]{"role_id"})
.build();

String bearerToken = token.getBearerToken();
assertEquals(bearerToken,"valid.bearer.token");
} catch (SkyflowException | IOException | ParseException exception) {
assertNull(exception);
}
}


}
Original file line number Diff line number Diff line change
@@ -8,13 +8,17 @@
import com.skyflow.entities.LogLevel;
import com.skyflow.errors.ErrorCode;
import com.skyflow.errors.SkyflowException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

public class SignedDataTokensTest {

@@ -156,20 +160,37 @@ public void testNoDataTokenProvided() {
}
}

@Test
public void testValidSignedTokenWithTTL() {
try {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(VALID_CREDENTIALS_FILE_PATH));
JSONObject saCreds = (JSONObject) obj;
SignedDataTokens token = new SignedDataTokens.SignedDataTokensBuilder()
.setCredentials(saCreds.toJSONString())
.setCtx("abc")
.setTimeToLive(30)
.setDataTokens(new String[] { "5230-0316-0674-5728" }).build();
List<SignedDataTokenResponse> signedToken = token.getSignedDataTokens();
Assert.assertNotNull(signedToken);

} catch (SkyflowException | IOException | ParseException exception) {
assertNull(exception);
}
}

@Test
public void testValidSignedToken() {
String creds = System.getProperty("TEST_DATA_CREDENTIALS_FILE");
SignedDataTokens token = new SignedDataTokens.SignedDataTokensBuilder()
.setCredentials(creds)
.setCtx("abc")
.setTimeToLive(30)
.setDataTokens(new String[] { "5230-0316-0674-5728" }).build();
try {
SignedDataTokens token = new SignedDataTokens.SignedDataTokensBuilder()
.setCredentials(new File(VALID_CREDENTIALS_FILE_PATH))
.setCtx("abc")
.setDataTokens(new String[] { "5230-0316-0674-5728" }).build();
List<SignedDataTokenResponse> signedToken = token.getSignedDataTokens();
Assert.assertNotNull(signedToken);

} catch (SkyflowException exception) {
Assert.assertNull(exception);
assertNull(exception);
}
}

0 comments on commit d0517a6

Please sign in to comment.