Skip to content

Commit

Permalink
fix: CheckIdentityKeyExists was comparing to constant (#192)
Browse files Browse the repository at this point in the history
Signed-off-by: Bryon Nevis <[email protected]>
  • Loading branch information
bnevis-i authored Mar 7, 2023
1 parent 9c30f8a commit 3201b0f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/pkg/vault/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func (c *Client) CheckIdentityKeyExists(token string, keyName string) (bool, err
}

for _, v := range response.Data.Keys {
if v == "keyName" {
if v == keyName {
return true, nil
}
}
Expand Down
54 changes: 54 additions & 0 deletions internal/pkg/vault/management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,60 @@ func TestCreateRole(t *testing.T) {
}
}

func TestCheckIdentityKeyExists(t *testing.T) {
// Arrange
mockLogger := logger.MockLogger{}

ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "LIST", r.Method)
require.Equal(t, oidcKeyAPI, r.URL.EscapedPath())
require.Equal(t, expectedToken, r.Header.Get(AuthTypeHeader))

w.WriteHeader(http.StatusOK)
response := ListNamedKeysResponse{}
response.Data.Keys = []string{"service1", "service2"}
err := json.NewEncoder(w).Encode(response)
require.NoError(t, err)
}))
defer ts.Close()

client := createClient(t, ts.URL, mockLogger)

// Act
exist, err := client.CheckIdentityKeyExists(expectedToken, "service2")

// Assert
require.NoError(t, err)
require.Equal(t, true, exist)
}

func TestCheckIdentityKeyNotExists(t *testing.T) {
// Arrange
mockLogger := logger.MockLogger{}

ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "LIST", r.Method)
require.Equal(t, oidcKeyAPI, r.URL.EscapedPath())
require.Equal(t, expectedToken, r.Header.Get(AuthTypeHeader))

w.WriteHeader(http.StatusOK)
response := ListNamedKeysResponse{}
response.Data.Keys = []string{"service1", "service2"}
err := json.NewEncoder(w).Encode(response)
require.NoError(t, err)
}))
defer ts.Close()

client := createClient(t, ts.URL, mockLogger)

// Act
exist, err := client.CheckIdentityKeyExists(expectedToken, "service3")

// Assert
require.NoError(t, err)
require.Equal(t, false, exist)
}

func TestCreateOrUpdateIdentity(t *testing.T) {
// Arrange
mockLogger := logger.MockLogger{}
Expand Down

0 comments on commit 3201b0f

Please sign in to comment.