Skip to content

Commit 9e0c95b

Browse files
committed
add: test for apiKey endpoint
1 parent 7aa3e43 commit 9e0c95b

File tree

6 files changed

+101
-6
lines changed

6 files changed

+101
-6
lines changed

src/main/java/org/typesense/api/Client.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class Client {
1414
private HashMap<String, Alias> individualAliases;
1515

1616
private Keys keys;
17-
private HashMap<String, Key> individualKeys;
17+
private HashMap<Long, Key> individualKeys;
1818

1919
public Health health;
2020
public Operations operations;
@@ -30,7 +30,7 @@ public class Client {
3030
this.aliases = new Aliases(this.apiCall);
3131
this.individualAliases = new HashMap<>();
3232
this.keys = new Keys(this.apiCall);
33-
this.individualKeys = new HashMap<>();
33+
this.individualKeys = new HashMap<Long, Key>();
3434
this.health = new Health(this.apiCall);
3535
this.operations = new Operations(this.apiCall);
3636
this.metrics = new Metrics(this.apiCall);
@@ -74,7 +74,7 @@ public Keys keys(){
7474
return this.keys;
7575
}
7676

77-
public Key keys(String id){
77+
public Key keys(Long id){
7878
Key retVal;
7979

8080
if(!this.individualKeys.containsKey(id)){

src/main/java/org/typesense/api/Key.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
public class Key {
66

7-
private String id;
7+
private Long id;
88
private ApiCall apiCall;
99

10-
public Key(String id, ApiCall apiCall) {
10+
public Key(Long id, ApiCall apiCall) {
1111
this.id = id;
1212
this.apiCall = apiCall;
1313
}

src/main/java/org/typesense/api/Keys.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public Keys(ApiCall apiCall) {
2222
}
2323

2424
public ApiKey create(ApiKeySchema apiKeySchema){
25+
if (apiKeySchema.getExpiresAt() == null) {
26+
apiKeySchema.setExpiresAt(System.currentTimeMillis() / 1000L + 315360000); // Adding 10 years for expiration.
27+
}
2528
return this.apiCall.post(Keys.RESOURCEPATH, apiKeySchema, ApiKey.class);
2629
}
2730

src/main/java/org/typesense/model/ApiKeySchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class ApiKeySchema {
2626
@Schema(required = true, description = "")
2727
private List<String> collections = new ArrayList<String>();
2828

29-
@Schema(required = true, description = "")
29+
@Schema(description = "")
3030
private Long expiresAt = null;
3131
/**
3232
* Get description

src/test/java/org/typesense/api/Helper.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.time.Duration;
77
import java.util.ArrayList;
88
import java.util.HashMap;
9+
import java.util.List;
910

1011
public class Helper {
1112
private final Client client;
@@ -52,6 +53,16 @@ public void createTestAlias() {
5253
client.aliases().upsert("books", collectionAliasSchema);
5354
}
5455

56+
public ApiKey createTestKey() {
57+
ApiKeySchema apiKeySchema = new ApiKeySchema();
58+
List<String> actionValues = new ArrayList<>();
59+
List<String> collectionValues = new ArrayList<>();
60+
actionValues.add("*");
61+
collectionValues.add("*");
62+
apiKeySchema.description("Admin Key").actions(actionValues).collections(collectionValues);
63+
return client.keys().create(apiKeySchema);
64+
}
65+
5566
public void teardown() {
5667
CollectionResponse[] collectionResponses = client.collections().retrieve();
5768
for(CollectionResponse c:collectionResponses) {
@@ -62,5 +73,10 @@ public void teardown() {
6273
for(CollectionAlias a:collectionAliasesResponse.getAliases()) {
6374
client.aliases(a.getName()).delete();
6475
}
76+
77+
ApiKeysResponse apiKeysResponse = client.keys().retrieve();
78+
for (ApiKey k: apiKeysResponse.getKeys()) {
79+
client.keys(k.getId()).delete();
80+
}
6581
}
6682
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.typesense.api;
2+
3+
import jdk.javadoc.internal.doclets.toolkit.util.Utils;
4+
import junit.framework.TestCase;
5+
import org.typesense.model.ApiKey;
6+
import org.typesense.model.ApiKeySchema;
7+
8+
import java.util.ArrayList;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
12+
public class KeysTest extends TestCase {
13+
14+
private Client client;
15+
private Helper helper;
16+
private String testKey;
17+
private Long id;
18+
19+
public void setUp() throws Exception {
20+
super.setUp();
21+
helper = new Helper();
22+
client = helper.getClient();
23+
ApiKey key = helper.createTestKey();
24+
testKey = key.getValue();
25+
id = key.getId();
26+
}
27+
28+
public void tearDown() throws Exception {
29+
super.tearDown();
30+
helper.teardown();
31+
}
32+
33+
public void testCreate() {
34+
ApiKeySchema apiKeySchema = new ApiKeySchema();
35+
List<String> actionValues = new ArrayList<>();
36+
List<String> collectionValues = new ArrayList<>();
37+
38+
actionValues.add("*");
39+
collectionValues.add("*");
40+
41+
apiKeySchema.description("Admin Key").actions(actionValues).collections(collectionValues);
42+
43+
System.out.println(client.keys().create(apiKeySchema));
44+
}
45+
46+
public void testCreateSearchOnly(){
47+
ApiKeySchema apiKeySchema = new ApiKeySchema();
48+
List<String> actionValues = new ArrayList<>();
49+
List<String> collectionValues = new ArrayList<>();
50+
51+
actionValues.add("documents:search");
52+
collectionValues.add("books");
53+
54+
apiKeySchema.description("Search only Key").actions(actionValues).collections(collectionValues);
55+
56+
System.out.println(this.client.keys().create(apiKeySchema));
57+
}
58+
59+
public void testRetrieve() {
60+
System.out.println(this.client.keys(id).retrieve());
61+
}
62+
63+
public void testRetrieveAll() {
64+
System.out.println(client.keys().retrieve());
65+
}
66+
67+
public void testDelete(){
68+
System.out.println(this.client.keys(id).delete());
69+
}
70+
71+
public void testScopedKey(){
72+
HashMap<String, Object> parameters = new HashMap<>();
73+
parameters.put("filter_by", "company_id:124");
74+
System.out.println(this.client.keys().generateScopedSearchKey(testKey,parameters));
75+
}
76+
}

0 commit comments

Comments
 (0)