terraform-provider-stackitp.../stackit/internal/validate/validate_test.go
Henrique Santos 248b9834ff
Object storage misc fixes (#82)
* Fix wrong reference

* Fix schema

* Fix mapFields not fetching credentials group id

* Change expiration timestamp

* Fix schema

* Remove fields that don't come in the GET response

* Add RFC3339SecondsOnly

* Change expiration timestamp to not support fractional seconds

* Set retry timeout

* Harmonize expiration timestamp

* Skip import check on credential keys

* Add error check

* Update docs

* Change field description

* Add test case, simplify test

* Add test case, simplify test

* Rename variable

* Generate docs

---------

Co-authored-by: Henrique Santos <henrique.santos@freiheit.com>
2023-10-13 15:02:48 +01:00

264 lines
4.4 KiB
Go

package validate
import (
"context"
"testing"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
func TestUUID(t *testing.T) {
tests := []struct {
description string
input string
isValid bool
}{
{
"ok",
"cae27bba-c43d-498a-861e-d11d241c4ff8",
true,
},
{
"too short",
"a-b-c-d",
false,
},
{
"Empty",
"",
false,
},
{
"not UUID",
"www-541-%",
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
r := validator.StringResponse{}
UUID().ValidateString(context.Background(), validator.StringRequest{
ConfigValue: types.StringValue(tt.input),
}, &r)
if !tt.isValid && !r.Diagnostics.HasError() {
t.Fatalf("Should have failed")
}
if tt.isValid && r.Diagnostics.HasError() {
t.Fatalf("Should not have failed: %v", r.Diagnostics.Errors())
}
})
}
}
func TestIP(t *testing.T) {
tests := []struct {
description string
input string
isValid bool
}{
{
"ok IP4",
"111.222.111.222",
true,
},
{
"ok IP6",
"2001:0db8:85a3:08d3::0370:7344",
true,
},
{
"too short",
"0.1.2",
false,
},
{
"Empty",
"",
false,
},
{
"Not an IP",
"for-sure-not-an-IP",
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
r := validator.StringResponse{}
IP().ValidateString(context.Background(), validator.StringRequest{
ConfigValue: types.StringValue(tt.input),
}, &r)
if !tt.isValid && !r.Diagnostics.HasError() {
t.Fatalf("Should have failed")
}
if tt.isValid && r.Diagnostics.HasError() {
t.Fatalf("Should not have failed: %v", r.Diagnostics.Errors())
}
})
}
}
func TestNoSeparator(t *testing.T) {
tests := []struct {
description string
input string
isValid bool
}{
{
"ok",
"ABCD",
true,
},
{
"ok-2",
"#$%&/()=.;-",
true,
},
{
"Empty",
"",
true,
},
{
"not ok",
"ab,",
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
r := validator.StringResponse{}
NoSeparator().ValidateString(context.Background(), validator.StringRequest{
ConfigValue: types.StringValue(tt.input),
}, &r)
if !tt.isValid && !r.Diagnostics.HasError() {
t.Fatalf("Should have failed")
}
if tt.isValid && r.Diagnostics.HasError() {
t.Fatalf("Should not have failed: %v", r.Diagnostics.Errors())
}
})
}
}
func TestMinorVersionNumber(t *testing.T) {
tests := []struct {
description string
input string
isValid bool
}{
{
"ok",
"1.20",
true,
},
{
"ok-2",
"1.3",
true,
},
{
"ok-3",
"10.1",
true,
},
{
"Empty",
"",
false,
},
{
"not ok",
"afssfdfs",
false,
},
{
"not ok-major-version",
"1",
false,
},
{
"not ok-patch-version",
"1.20.1",
false,
},
{
"not ok-version",
"v1.20.1",
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
r := validator.StringResponse{}
MinorVersionNumber().ValidateString(context.Background(), validator.StringRequest{
ConfigValue: types.StringValue(tt.input),
}, &r)
if !tt.isValid && !r.Diagnostics.HasError() {
t.Fatalf("Should have failed")
}
if tt.isValid && r.Diagnostics.HasError() {
t.Fatalf("Should not have failed: %v", r.Diagnostics.Errors())
}
})
}
}
func TestRFC3339SecondsOnly(t *testing.T) {
tests := []struct {
description string
input string
isValid bool
}{
{
"ok",
"9999-01-02T03:04:05Z",
true,
},
{
"ok_2",
"9999-01-02T03:04:05+06:00",
true,
},
{
"empty",
"",
false,
},
{
"not_ok",
"foo-bar",
false,
},
{
"with_sub_seconds",
"9999-01-02T03:04:05.678Z",
false,
},
{
"with_sub_seconds_2",
"9999-01-02T03:04:05.678+06:00",
false,
},
}
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
r := validator.StringResponse{}
RFC3339SecondsOnly().ValidateString(context.Background(), validator.StringRequest{
ConfigValue: types.StringValue(tt.input),
}, &r)
if !tt.isValid && !r.Diagnostics.HasError() {
t.Fatalf("Should have failed")
}
if tt.isValid && r.Diagnostics.HasError() {
t.Fatalf("Should not have failed: %v", r.Diagnostics.Errors())
}
})
}
}