Skip to content

Commit

Permalink
Modify test helper func + add additional tests and other minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrinalirao committed Nov 3, 2022
1 parent 7aaf233 commit d4ae7c4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,8 @@

## Enhancements

* Add OPA support to the Policy Set API's by @mrinalirao [#575](https://github.com/hashicorp/go-tfe/pull/575)
* Add OPA support to the Policy Set APIs by @mrinalirao [#575](https://github.com/hashicorp/go-tfe/pull/575)
* Add OPA support to the Policy APIs by @mrinalirao [#579](https://github.com/hashicorp/go-tfe/pull/579)

# v1.12.0

Expand Down
2 changes: 1 addition & 1 deletion errors.go
Expand Up @@ -199,7 +199,7 @@ var (

ErrRequiredName = errors.New("name is required")

ErrRequiredQuery = errors.New("invalid attribute\n\nQuery can't be blank")
ErrRequiredQuery = errors.New("query cannot be empty")

ErrRequiredEnabled = errors.New("enabled is required")

Expand Down
4 changes: 2 additions & 2 deletions helper_test.go
Expand Up @@ -614,7 +614,7 @@ func createPolicy(t *testing.T, client *Client, org *Organization) (*Policy, fun
}
}

func createPolicyWithOptions(t *testing.T, client *Client, org *Organization, opts *PolicyCreateOptions) (*Policy, func()) {
func createPolicyWithOptions(t *testing.T, client *Client, org *Organization, opts PolicyCreateOptions) (*Policy, func()) {
var orgCleanup func()

if org == nil {
Expand Down Expand Up @@ -677,7 +677,7 @@ func createUploadedPolicy(t *testing.T, client *Client, pass bool, org *Organiza
}
}

func createUploadedPolicyWithOptions(t *testing.T, client *Client, pass bool, org *Organization, opts *PolicyCreateOptions) (*Policy, func()) {
func createUploadedPolicyWithOptions(t *testing.T, client *Client, pass bool, org *Organization, opts PolicyCreateOptions) (*Policy, func()) {
var orgCleanup func()

if org == nil {
Expand Down
5 changes: 4 additions & 1 deletion policy.go
Expand Up @@ -111,7 +111,7 @@ type PolicyCreateOptions struct {
Name *string `jsonapi:"attr,name"`

// **Note: This field is still in BETA and subject to change.**
// Optional: The underlying technology that the policy supports.
// Optional: The underlying technology that the policy supports. Defaults to Sentinel if not specified for PolicyCreate.
Kind PolicyKind `jsonapi:"attr,kind,omitempty"`

// **Note: This field is still in BETA and subject to change.**
Expand Down Expand Up @@ -289,6 +289,9 @@ func (o PolicyCreateOptions) valid() error {
if !validStringID(o.Name) {
return ErrInvalidName
}
if o.Kind == OPA && !validString(o.Query) {
return ErrRequiredQuery
}
if o.Enforce == nil {
return ErrRequiredEnforce
}
Expand Down
21 changes: 18 additions & 3 deletions policy_integration_beta_test.go
Expand Up @@ -193,7 +193,7 @@ func TestPoliciesCreate_Beta(t *testing.T) {
assert.Equal(t, err, ErrRequiredEnforcementPath)
})

t.Run("when options is missing enforcement path", func(t *testing.T) {
t.Run("when options is missing enforcement mode", func(t *testing.T) {
name := randomString(t)
options := PolicyCreateOptions{
Name: String(name),
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestPoliciesList_Beta(t *testing.T) {
defer pTestCleanup1()
pTest2, pTestCleanup2 := createPolicy(t, client, orgTest)
defer pTestCleanup2()
opaOptions := &PolicyCreateOptions{
opaOptions := PolicyCreateOptions{
Kind: OPA,
Query: String("terraform.policy1.deny"),
Enforce: []*EnforcementOptions{
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestPoliciesUpdate_Beta(t *testing.T) {
defer orgTestCleanup()

t.Run("with a new query", func(t *testing.T) {
options := &PolicyCreateOptions{
options := PolicyCreateOptions{
Description: String("A sample policy"),
Kind: OPA,
Query: String("terraform.main"),
Expand All @@ -346,4 +346,19 @@ func TestPoliciesUpdate_Beta(t *testing.T) {
assert.NotEqual(t, *pBefore.Query, *pAfter.Query)
assert.Equal(t, "terraform.policy1.deny", *pAfter.Query)
})

t.Run("update query when kind is not OPA", func(t *testing.T) {
pBefore, pBeforeCleanup := createUploadedPolicy(t, client, true, orgTest)
defer pBeforeCleanup()

pAfter, err := client.Policies.Update(ctx, pBefore.ID, PolicyUpdateOptions{
Query: String("terraform.policy1.deny"),
})
require.NoError(t, err)

assert.Equal(t, pBefore.Name, pAfter.Name)
assert.Equal(t, pBefore.Enforce, pAfter.Enforce)
assert.Equal(t, Sentinel, pAfter.Kind)
assert.Nil(t, pAfter.Query)
})
}

0 comments on commit d4ae7c4

Please sign in to comment.