Skip to content
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

chore(no-ticket): validate provided credentials in the plan phase #129

Merged
merged 2 commits into from
Dec 3, 2024
Merged
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
5 changes: 5 additions & 0 deletions cloudsmith/provider_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func newProviderConfig(apiHost, apiKey, userAgent string) (*providerConfig, diag
},
)

req := apiClient.UserApi.UserSelf(auth)
if _, _, err := apiClient.UserApi.UserSelfExecute(req); err != nil {
return nil, diag.FromErr(errors.New("invalid API credentials"))
}

return &providerConfig{Auth: auth, APIClient: apiClient}, nil
}

Expand Down
62 changes: 61 additions & 1 deletion cloudsmith/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
package cloudsmith

import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -31,8 +36,63 @@ func testAccPreCheck(t *testing.T) {
if v := os.Getenv("CLOUDSMITH_API_KEY"); v == "" {
t.Fatal("CLOUDSMITH_API_KEY must be set for acceptance tests")
}

if v := os.Getenv("CLOUDSMITH_NAMESPACE"); v == "" {
t.Fatal("CLOUDSMITH_NAMESPACE must be set for acceptance tests")
}
}
func TestAccProvider_UserSelfValidation(t *testing.T) {
// Create mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/user/self/" {
w.Header().Set("Content-Type", "application/json")
if r.Header.Get("X-Api-Key") == "valid-token" {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `{"email": "[email protected]", "name": "Test User", "slug": "test-user", "slug_perm": "test-user"}`)
} else {
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintln(w, `{"error": "invalid API credentials"}`)
}
}
}))
defer server.Close()

tests := []struct {
name string
apiKey string
}{
{
name: "ValidToken",
apiKey: "valid-token",
},
{
name: "InvalidToken",
apiKey: "invalid-token",
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Setenv("CLOUDSMITH_API_HOST", server.URL)
t.Setenv("CLOUDSMITH_API_KEY", tc.apiKey)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: selfConfig,
ExpectError: regexp.MustCompile("invalid API credentials"),
SkipFunc: func() (bool, error) {
// Skip error check for valid token case
return tc.apiKey == "valid-token", nil
},
},
},
})
})
}
}

var selfConfig string = `
data "cloudsmith_user_self" "this" {
}`
17 changes: 10 additions & 7 deletions cloudsmith/resource_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cloudsmith
import (
"fmt"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
Expand Down Expand Up @@ -32,7 +33,7 @@ func TestAccService_basic(t *testing.T) {
testAccServiceCheckExists("cloudsmith_service.test"),
// check a sample of computed properties have been set correctly
resource.TestCheckResourceAttr("cloudsmith_service.test", "description", ""),
resource.TestCheckResourceAttr("cloudsmith_service.test", "slug", "tf-test-service"),
resource.TestMatchResourceAttr("cloudsmith_service.test", "slug", regexp.MustCompile("^tf-test-service.*$")),
resource.TestCheckResourceAttrSet("cloudsmith_service.test", "key"),
resource.TestCheckResourceAttr("cloudsmith_service.test", "role", "Member"),
resource.TestCheckNoResourceAttr("cloudsmith_service.test", "team.#"),
Expand Down Expand Up @@ -60,13 +61,15 @@ func TestAccService_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccServiceCheckExists("cloudsmith_service.test"),
resource.TestCheckResourceAttrSet("cloudsmith_service.test", "team.#"),
resource.TestCheckTypeSetElemNestedAttrs("cloudsmith_service.test", "team.*", map[string]string{
"slug": "tf-test-team-svc",
"role": "Member",

resource.TestMatchTypeSetElemNestedAttrs("cloudsmith_service.test", "team.*", map[string]*regexp.Regexp{
"slug": regexp.MustCompile("^tf-test-team-svc(-[^2].*)?$"),
"role": regexp.MustCompile("^Member$"),
}),
resource.TestCheckTypeSetElemNestedAttrs("cloudsmith_service.test", "team.*", map[string]string{
"slug": "tf-test-team-svc-2",
"role": "Manager",

resource.TestMatchTypeSetElemNestedAttrs("cloudsmith_service.test", "team.*", map[string]*regexp.Regexp{
"slug": regexp.MustCompile("^tf-test-team-svc-2.*$"),
"role": regexp.MustCompile("^Manager$"),
}),
),
},
Expand Down