Skip to content

Add support for keyless lookups #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions acceptance/fixtures/var/lib/jerakia/data/common/keyless.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo: bar
hello: world
23 changes: 23 additions & 0 deletions acceptance/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,26 @@ func TestLookupMetadata(t *testing.T) {
expected := fixtures.LookupMetadataResult
assert.Equal(t, expected, *actual)
}

func TestLookupKeyless(t *testing.T) {
if v := os.Getenv("JERAKIA_ACC"); v == "" {
t.Skip("JERAKIA_ACC not set")
}

client, err := NewClient()
if err != nil {
t.Fatal(err)
}

lookupOpts := &jerakia.LookupOpts{
Namespace: "keyless",
}

actual, err := jerakia.Lookup(client, "", lookupOpts)
if err != nil {
t.Fatal(err)
}

expected := fixtures.LookupKeylessResult
assert.Equal(t, expected, *actual)
}
6 changes: 5 additions & 1 deletion lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,11 @@ type LookupResult struct {
// Lookup performs a lookup.
func Lookup(client *Client, key string, opts *LookupOpts) (*LookupResult, error) {
var r LookupResult
url := client.config.URL + "/" + LookupURL + "/" + key
url := client.config.URL + "/" + LookupURL

if key != "" {
url += ("/" + key)
}

if opts != nil {
query, err := opts.ToLookupQuery()
Expand Down
37 changes: 37 additions & 0 deletions testing/lookup_fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,40 @@ func HandleLookupMetadata(t *testing.T) {
fmt.Fprintf(w, LookupMetadataResponse)
})
}

// LookupKeylessResponse is the expected response of a keyless lookup.
const LookupKeylessResponse = `
{
"found": true,
"payload": {
"foo": "bar",
"hello": "world"
},
"status": "ok"
}
`

// LookupKeylessResult is the expected result of a keyless lookup.
var LookupKeylessResult = jerakia.LookupResult{
Status: "ok",
Found: true,
Payload: map[string]interface{}{
"foo": "bar",
"hello": "world",
},
}

// HandleLookupKeyless tests a keyless lookup.
func HandleLookupKeyless(t *testing.T) {
th.Mux.HandleFunc("/lookup", func(w http.ResponseWriter, r *http.Request) {

assert.Equal(t, "GET", r.Method)
assert.Equal(t, fake.Token, r.Header.Get("X-Authentication"))
assert.Equal(t, []string{"keyless"}, r.URL.Query()["namespace"])

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, LookupKeylessResponse)
})
}

18 changes: 18 additions & 0 deletions testing/lookup_requests_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,21 @@ func TestLookupMetadata(t *testing.T) {
expected := LookupMetadataResult
assert.Equal(t, expected, *actual)
}

func TestLookupKeyless(t *testing.T) {
th.SetupHTTP()
defer th.TeardownHTTP()
HandleLookupKeyless(t)

lookupOpts := &jerakia.LookupOpts{
Namespace: "keyless",
}

actual, err := jerakia.Lookup(fake.FakeClient(), "", lookupOpts)
if err != nil {
t.Fatal(err)
}

expected := LookupKeylessResult
assert.Equal(t, expected, *actual)
}