Skip to content

Commit c6889cb

Browse files
author
Ruxandra Fediuc
committed
feat: add support for deleting config by key
1 parent 3a302e3 commit c6889cb

File tree

3 files changed

+164
-102
lines changed

3 files changed

+164
-102
lines changed

configs_test.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package ctoai
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"net/http/httptest"
8+
"reflect"
9+
"testing"
10+
)
11+
12+
func TestGetConfig(t *testing.T) {
13+
expectedResponse := `{"value": "config-value"}`
14+
expectedBody := map[string]interface{}{
15+
"key": "test-key",
16+
}
17+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
18+
ValidateRequest(t, r, "/config/get")
19+
20+
var tmp map[string]interface{}
21+
err := json.NewDecoder(r.Body).Decode(&tmp)
22+
if err != nil {
23+
t.Errorf("Error in decoding response body: %s", err)
24+
}
25+
26+
if !reflect.DeepEqual(tmp, expectedBody) {
27+
t.Errorf("Error unexpected request body: %+v", tmp)
28+
}
29+
30+
fmt.Fprintf(w, expectedResponse)
31+
}))
32+
33+
defer ts.Close()
34+
35+
SetPortVar(t, ts)
36+
37+
s := NewSdk()
38+
output, err := s.GetConfig("test-key")
39+
if err != nil {
40+
t.Errorf("Error in config request: %v", err)
41+
}
42+
43+
if output != "config-value" {
44+
t.Errorf("Error unexpected output: %v", output)
45+
}
46+
}
47+
48+
func TestGetConfig_Null(t *testing.T) {
49+
expectedResponse := `{"value": null}`
50+
expectedBody := map[string]interface{}{
51+
"key": "test-key",
52+
}
53+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
54+
ValidateRequest(t, r, "/config/get")
55+
56+
var tmp map[string]interface{}
57+
err := json.NewDecoder(r.Body).Decode(&tmp)
58+
if err != nil {
59+
t.Errorf("Error in decoding response body: %s", err)
60+
}
61+
62+
if !reflect.DeepEqual(tmp, expectedBody) {
63+
t.Errorf("Error unexpected request body: %+v", tmp)
64+
}
65+
66+
fmt.Fprintf(w, expectedResponse)
67+
}))
68+
69+
defer ts.Close()
70+
71+
SetPortVar(t, ts)
72+
73+
s := NewSdk()
74+
output, err := s.GetConfig("test-key")
75+
if err != nil {
76+
t.Errorf("Error in config request: %v", err)
77+
}
78+
79+
if output != "" {
80+
t.Errorf("Error unexpected output: %v", output)
81+
}
82+
}
83+
84+
func TestSetConfig(t *testing.T) {
85+
expectedBody := map[string]interface{}{
86+
"key": "key-of-value",
87+
"value": "value-of-key",
88+
}
89+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
90+
ValidateRequest(t, r, "/config/set")
91+
92+
var tmp map[string]interface{}
93+
err := json.NewDecoder(r.Body).Decode(&tmp)
94+
if err != nil {
95+
t.Errorf("Error in decoding response body: %s", err)
96+
}
97+
98+
if !reflect.DeepEqual(tmp, expectedBody) {
99+
t.Errorf("Error unexpected request body: %+v", tmp)
100+
}
101+
}))
102+
103+
defer ts.Close()
104+
105+
SetPortVar(t, ts)
106+
107+
s := NewSdk()
108+
err := s.SetConfig("key-of-value", "value-of-key")
109+
if err != nil {
110+
t.Errorf("Error in config request: %v", err)
111+
}
112+
}
113+
114+
func TestDeleteConfig(t *testing.T) {
115+
expectedResponse := `{"value": true}`
116+
expectedBody := map[string]interface{}{
117+
"key": "test-key",
118+
}
119+
120+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
121+
ValidateRequest(t, r, "/config/delete")
122+
123+
var tmp map[string]interface{}
124+
err := json.NewDecoder(r.Body).Decode(&tmp)
125+
if err != nil {
126+
t.Errorf("Error in decoding response body: %s", err)
127+
}
128+
129+
if !reflect.DeepEqual(tmp, expectedBody) {
130+
t.Errorf("Error unexpected request body: %+v", tmp)
131+
}
132+
133+
fmt.Fprintf(w, expectedResponse)
134+
}))
135+
136+
defer ts.Close()
137+
138+
SetPortVar(t, ts)
139+
140+
s := NewSdk()
141+
output, err := s.DeleteConfig("test-key")
142+
if err != nil {
143+
t.Errorf("Error in config request: %v", err)
144+
}
145+
146+
if output != true {
147+
t.Errorf("Error unexpected output: %v", output)
148+
}
149+
}

sdk.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ func (s *Sdk) SetConfig(key string, value string) error {
137137
})
138138
}
139139

140+
// DeleteConfig deletes a value from the config (user-specific) key/value store
141+
// Returns false if key not found, true if success
142+
func (s *Sdk) DeleteConfig(key string) (bool, error) {
143+
daemonValue, err := daemon.SyncRequest("config/delete", map[string]string{"key": key})
144+
if err != nil {
145+
return false, err
146+
}
147+
148+
valueDeleted, ok := daemonValue.(bool)
149+
if !ok {
150+
return false, fmt.Errorf("Received non-boolean JSON %v", daemonValue)
151+
}
152+
return valueDeleted, nil
153+
}
154+
140155
// GetSecret requests a secret from the secret store by key.
141156
//
142157
// If the secret exists, it is returned, with the daemon notifying the user that it is in use.

state_test.go

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -74,105 +74,3 @@ func TestSetState(t *testing.T) {
7474
t.Errorf("Error in state request: %v", err)
7575
}
7676
}
77-
78-
func TestGetConfig(t *testing.T) {
79-
expectedResponse := `{"value": "config-value"}`
80-
expectedBody := map[string]interface{}{
81-
"key": "test-key",
82-
}
83-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
84-
ValidateRequest(t, r, "/config/get")
85-
86-
var tmp map[string]interface{}
87-
err := json.NewDecoder(r.Body).Decode(&tmp)
88-
if err != nil {
89-
t.Errorf("Error in decoding response body: %s", err)
90-
}
91-
92-
if !reflect.DeepEqual(tmp, expectedBody) {
93-
t.Errorf("Error unexpected request body: %+v", tmp)
94-
}
95-
96-
fmt.Fprintf(w, expectedResponse)
97-
}))
98-
99-
defer ts.Close()
100-
101-
SetPortVar(t, ts)
102-
103-
s := NewSdk()
104-
output, err := s.GetConfig("test-key")
105-
if err != nil {
106-
t.Errorf("Error in config request: %v", err)
107-
}
108-
109-
if output != "config-value" {
110-
t.Errorf("Error unexpected output: %v", output)
111-
}
112-
}
113-
114-
func TestGetConfig_Null(t *testing.T) {
115-
expectedResponse := `{"value": null}`
116-
expectedBody := map[string]interface{}{
117-
"key": "test-key",
118-
}
119-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
120-
ValidateRequest(t, r, "/config/get")
121-
122-
var tmp map[string]interface{}
123-
err := json.NewDecoder(r.Body).Decode(&tmp)
124-
if err != nil {
125-
t.Errorf("Error in decoding response body: %s", err)
126-
}
127-
128-
if !reflect.DeepEqual(tmp, expectedBody) {
129-
t.Errorf("Error unexpected request body: %+v", tmp)
130-
}
131-
132-
fmt.Fprintf(w, expectedResponse)
133-
}))
134-
135-
defer ts.Close()
136-
137-
SetPortVar(t, ts)
138-
139-
s := NewSdk()
140-
output, err := s.GetConfig("test-key")
141-
if err != nil {
142-
t.Errorf("Error in config request: %v", err)
143-
}
144-
145-
if output != "" {
146-
t.Errorf("Error unexpected output: %v", output)
147-
}
148-
}
149-
150-
func TestSetConfig(t *testing.T) {
151-
expectedBody := map[string]interface{}{
152-
"key": "key-of-value",
153-
"value": "value-of-key",
154-
}
155-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
156-
ValidateRequest(t, r, "/config/set")
157-
158-
var tmp map[string]interface{}
159-
err := json.NewDecoder(r.Body).Decode(&tmp)
160-
if err != nil {
161-
t.Errorf("Error in decoding response body: %s", err)
162-
}
163-
164-
if !reflect.DeepEqual(tmp, expectedBody) {
165-
t.Errorf("Error unexpected request body: %+v", tmp)
166-
}
167-
}))
168-
169-
defer ts.Close()
170-
171-
SetPortVar(t, ts)
172-
173-
s := NewSdk()
174-
err := s.SetConfig("key-of-value", "value-of-key")
175-
if err != nil {
176-
t.Errorf("Error in config request: %v", err)
177-
}
178-
}

0 commit comments

Comments
 (0)