|
@@ -2,90 +2,93 @@ package server
|
|
|
|
|
|
import (
|
|
|
"cfTest/cloudflareApi/zone"
|
|
|
- "reflect"
|
|
|
-
|
|
|
"context"
|
|
|
+ "github.com/cloudflare/cloudflare-go"
|
|
|
"github.com/samber/lo"
|
|
|
"log"
|
|
|
+ "reflect"
|
|
|
"testing"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
-//func TestCreateRuleSet(t *testing.T) {
|
|
|
-// type args struct {
|
|
|
-// zoneID string
|
|
|
-// rulesetPhase string
|
|
|
-// ruleset cloudflare.Ruleset
|
|
|
-// }
|
|
|
+// func TestCreateRuleSet(t *testing.T) {
|
|
|
+// type args struct {
|
|
|
+// zoneID string
|
|
|
+// rulesetPhase string
|
|
|
+// ruleset cloudflare.Ruleset
|
|
|
+// }
|
|
|
//
|
|
|
-// var (
|
|
|
-// True = true
|
|
|
-// CONST604800 = uint(604800)
|
|
|
-// )
|
|
|
+// var (
|
|
|
+// True = true
|
|
|
+// CONST604800 = uint(604800)
|
|
|
+// )
|
|
|
//
|
|
|
-// tests := []struct {
|
|
|
-// name string
|
|
|
-// args args
|
|
|
-// want interface{}
|
|
|
-// wantErr bool
|
|
|
-// }{
|
|
|
-// {
|
|
|
-// name: "Test1",
|
|
|
-// args: args{
|
|
|
-// zoneID: "e29551894461bf16e43a113e5ab09ed3",
|
|
|
-// rulesetPhase: "http_request_cache_settings",
|
|
|
-// ruleset: cloudflare.Ruleset{
|
|
|
-// Rules: []cloudflare.RulesetRule{
|
|
|
-// {
|
|
|
-// Expression: "(starts_with(http.request.uri.path, \"/\"))",
|
|
|
-// Enabled: true,
|
|
|
-// Description: "test1",
|
|
|
-// Action: "set_cache_settings",
|
|
|
-// ActionParameters: &cloudflare.RulesetRuleActionParameters{
|
|
|
-// Cache: &True,
|
|
|
-// CacheKey: &cloudflare.RulesetRuleActionParametersCacheKey{
|
|
|
-// CustomKey: &cloudflare.RulesetRuleActionParametersCustomKey{
|
|
|
-// Query: &cloudflare.RulesetRuleActionParametersCustomKeyQuery{
|
|
|
-// Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{
|
|
|
-// List: []string{
|
|
|
-// "abc",
|
|
|
+// tests := []struct {
|
|
|
+// name string
|
|
|
+// args args
|
|
|
+// want interface{}
|
|
|
+// wantErr bool
|
|
|
+// }{
|
|
|
+// {
|
|
|
+// name: "Test1",
|
|
|
+// args: args{
|
|
|
+// zoneID: "e29551894461bf16e43a113e5ab09ed3",
|
|
|
+// rulesetPhase: "http_request_cache_settings",
|
|
|
+// ruleset: cloudflare.Ruleset{
|
|
|
+// Rules: []cloudflare.RulesetRule{
|
|
|
+// {
|
|
|
+// Expression: "(starts_with(http.request.uri.path, \"/\"))",
|
|
|
+// Enabled: true,
|
|
|
+// Description: "test1",
|
|
|
+// Action: "set_cache_settings",
|
|
|
+// ActionParameters: &cloudflare.RulesetRuleActionParameters{
|
|
|
+// Cache: &True,
|
|
|
+// CacheKey: &cloudflare.RulesetRuleActionParametersCacheKey{
|
|
|
+// CustomKey: &cloudflare.RulesetRuleActionParametersCustomKey{
|
|
|
+// Query: &cloudflare.RulesetRuleActionParametersCustomKeyQuery{
|
|
|
+// Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{
|
|
|
+// List: []string{
|
|
|
+// "abc",
|
|
|
+// },
|
|
|
+// All: false,
|
|
|
// },
|
|
|
-// All: false,
|
|
|
// },
|
|
|
// },
|
|
|
// },
|
|
|
-// },
|
|
|
-// EdgeTTL: &cloudflare.RulesetRuleActionParametersEdgeTTL{
|
|
|
-// Mode: "override_origin",
|
|
|
-// Default: &CONST604800,
|
|
|
+// EdgeTTL: &cloudflare.RulesetRuleActionParametersEdgeTTL{
|
|
|
+// Mode: "override_origin",
|
|
|
+// Default: &CONST604800,
|
|
|
+// },
|
|
|
// },
|
|
|
// },
|
|
|
// },
|
|
|
// },
|
|
|
// },
|
|
|
+// want: nil,
|
|
|
+// wantErr: false,
|
|
|
// },
|
|
|
-// want: nil,
|
|
|
-// wantErr: false,
|
|
|
-// },
|
|
|
+// }
|
|
|
+// for _, tt := range tests {
|
|
|
+// t.Run(tt.name, func(t *testing.T) {
|
|
|
+// got, err := UpdateRuleSet("", "", tt.args.zoneID, tt.args.rulesetPhase, tt.args.ruleset)
|
|
|
+// if (err != nil) != tt.wantErr {
|
|
|
+// t.Errorf("UpdateRuleSet() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
+// return
|
|
|
+// }
|
|
|
+// if !reflect.DeepEqual(got, tt.want) {
|
|
|
+// t.Errorf("UpdateRuleSet() got = %v, want %v", got, tt.want)
|
|
|
+// }
|
|
|
+// })
|
|
|
+// }
|
|
|
// }
|
|
|
-// for _, tt := range tests {
|
|
|
-// t.Run(tt.name, func(t *testing.T) {
|
|
|
-// got, err := UpdateRuleSet("", "", tt.args.zoneID, tt.args.rulesetPhase, tt.args.ruleset)
|
|
|
-// if (err != nil) != tt.wantErr {
|
|
|
-// t.Errorf("UpdateRuleSet() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
-// return
|
|
|
-// }
|
|
|
-// if !reflect.DeepEqual(got, tt.want) {
|
|
|
-// t.Errorf("UpdateRuleSet() got = %v, want %v", got, tt.want)
|
|
|
-// }
|
|
|
-// })
|
|
|
+//
|
|
|
+// func newUint32(a uint32) *uint32 {
|
|
|
+// return &a
|
|
|
+// }
|
|
|
+//
|
|
|
+// func newStr(a string) *string {
|
|
|
+// return &a
|
|
|
// }
|
|
|
-//}
|
|
|
-//func newUint32(a uint32) *uint32 {
|
|
|
-// return &a
|
|
|
-//}
|
|
|
-//func newStr(a string) *string {
|
|
|
-// return &a
|
|
|
-//}
|
|
|
var (
|
|
|
apiKey = "2cc87f6213a4c40e5a90c8dced3c9c577eda5"
|
|
|
apiEmail = "ll@centcloud.net"
|
|
@@ -498,3 +501,131 @@ func TestZoneServer_CreateArgoSettingSubscription(t *testing.T) {
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func TestCreateZone(t *testing.T) {
|
|
|
+ api, err := cloudflare.New("d2589c4f617b0493357ae187a7b4518171d07", "shiqi@centcloud.net")
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ ctx := context.Background()
|
|
|
+ account, r, err := api.Account(ctx, "b9145a83318cae896ad9b23bdf1681a5")
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ t.Logf("Account Info:%+v", r)
|
|
|
+
|
|
|
+ createZone, err := api.CreateZone(ctx, "cf004.shiqi.website", false, account, "partial")
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ t.Logf("createZone: %+v", createZone)
|
|
|
+
|
|
|
+ time.Sleep(time.Second)
|
|
|
+
|
|
|
+ zoneId := createZone.ID
|
|
|
+
|
|
|
+ err = api.ZoneSetPlan(ctx, zoneId, "PARTNERS_ENT")
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestZoneServer_CreateZone(t *testing.T) {
|
|
|
+ type fields struct {
|
|
|
+ UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
|
|
|
+ }
|
|
|
+ type args struct {
|
|
|
+ ctx context.Context
|
|
|
+ requestCloudflare *zone.CreateZoneRequestCloudflare
|
|
|
+ }
|
|
|
+ tests := []struct {
|
|
|
+ name string
|
|
|
+ fields fields
|
|
|
+ args args
|
|
|
+ want *zone.CreateZoneReplyCloudflare
|
|
|
+ wantErr bool
|
|
|
+ }{
|
|
|
+ {
|
|
|
+ name: "测试用例1",
|
|
|
+ fields: fields{
|
|
|
+ UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
|
|
|
+ },
|
|
|
+ args: args{
|
|
|
+ ctx: context.Background(),
|
|
|
+ requestCloudflare: &zone.CreateZoneRequestCloudflare{
|
|
|
+ ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
|
|
|
+ ApiEmail: "shiqi@centcloud.net",
|
|
|
+ AccountId: "b9145a83318cae896ad9b23bdf1681a5",
|
|
|
+ Domain: "cf005.shiqi.website",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ want: nil,
|
|
|
+ wantErr: false,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ for _, tt := range tests {
|
|
|
+ t.Run(tt.name, func(t *testing.T) {
|
|
|
+ s := &ZoneServer{
|
|
|
+ UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
|
|
|
+ }
|
|
|
+ got, err := s.CreateZone(tt.args.ctx, tt.args.requestCloudflare)
|
|
|
+ if (err != nil) != tt.wantErr {
|
|
|
+ t.Errorf("CreateZone() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ t.Logf("CreateZone() got = %v", got)
|
|
|
+
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestZoneServer_SetZonePlan(t *testing.T) {
|
|
|
+ type fields struct {
|
|
|
+ UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
|
|
|
+ }
|
|
|
+ type args struct {
|
|
|
+ ctx context.Context
|
|
|
+ requestCloudflare *zone.SetZonePlanRequestCloudflare
|
|
|
+ }
|
|
|
+ tests := []struct {
|
|
|
+ name string
|
|
|
+ fields fields
|
|
|
+ args args
|
|
|
+ want *zone.SetZonePlanReplyCloudflare
|
|
|
+ wantErr bool
|
|
|
+ }{
|
|
|
+ {
|
|
|
+ name: "测试用例1",
|
|
|
+ fields: fields{
|
|
|
+ UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
|
|
|
+ },
|
|
|
+ args: args{
|
|
|
+ ctx: context.Background(),
|
|
|
+ requestCloudflare: &zone.SetZonePlanRequestCloudflare{
|
|
|
+ ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
|
|
|
+ ApiEmail: "shiqi@centcloud.net",
|
|
|
+ ZoneId: "19e7978bb9292fe11d3c75ff39f87685",
|
|
|
+ ZonePlan: "PARTNERS_ENT",
|
|
|
+ },
|
|
|
+ },
|
|
|
+ want: &zone.SetZonePlanReplyCloudflare{Success: true},
|
|
|
+ wantErr: false,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ for _, tt := range tests {
|
|
|
+ t.Run(tt.name, func(t *testing.T) {
|
|
|
+ s := &ZoneServer{
|
|
|
+ UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
|
|
|
+ }
|
|
|
+ got, err := s.SetZonePlan(tt.args.ctx, tt.args.requestCloudflare)
|
|
|
+ if (err != nil) != tt.wantErr {
|
|
|
+ t.Errorf("SetZonePlan() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !reflect.DeepEqual(got, tt.want) {
|
|
|
+ t.Errorf("SetZonePlan() got = %v, want %v", got, tt.want)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|