ZoneCloudflareServer_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. package server
  2. import (
  3. "cfTest/cloudflareApi/zone"
  4. "context"
  5. "github.com/cloudflare/cloudflare-go"
  6. "github.com/samber/lo"
  7. "log"
  8. "reflect"
  9. "testing"
  10. "time"
  11. )
  12. // func TestCreateRuleSet(t *testing.T) {
  13. // type args struct {
  14. // zoneID string
  15. // rulesetPhase string
  16. // ruleset cloudflare.Ruleset
  17. // }
  18. //
  19. // var (
  20. // True = true
  21. // CONST604800 = uint(604800)
  22. // )
  23. //
  24. // tests := []struct {
  25. // name string
  26. // args args
  27. // want interface{}
  28. // wantErr bool
  29. // }{
  30. // {
  31. // name: "Test1",
  32. // args: args{
  33. // zoneID: "e29551894461bf16e43a113e5ab09ed3",
  34. // rulesetPhase: "http_request_cache_settings",
  35. // ruleset: cloudflare.Ruleset{
  36. // Rules: []cloudflare.RulesetRule{
  37. // {
  38. // Expression: "(starts_with(http.request.uri.path, \"/\"))",
  39. // Enabled: true,
  40. // Description: "test1",
  41. // Action: "set_cache_settings",
  42. // ActionParameters: &cloudflare.RulesetRuleActionParameters{
  43. // Cache: &True,
  44. // CacheKey: &cloudflare.RulesetRuleActionParametersCacheKey{
  45. // CustomKey: &cloudflare.RulesetRuleActionParametersCustomKey{
  46. // Query: &cloudflare.RulesetRuleActionParametersCustomKeyQuery{
  47. // Exclude: &cloudflare.RulesetRuleActionParametersCustomKeyList{
  48. // List: []string{
  49. // "abc",
  50. // },
  51. // All: false,
  52. // },
  53. // },
  54. // },
  55. // },
  56. // EdgeTTL: &cloudflare.RulesetRuleActionParametersEdgeTTL{
  57. // Mode: "override_origin",
  58. // Default: &CONST604800,
  59. // },
  60. // },
  61. // },
  62. // },
  63. // },
  64. // },
  65. // want: nil,
  66. // wantErr: false,
  67. // },
  68. // }
  69. // for _, tt := range tests {
  70. // t.Run(tt.name, func(t *testing.T) {
  71. // got, err := UpdateRuleSet("", "", tt.args.zoneID, tt.args.rulesetPhase, tt.args.ruleset)
  72. // if (err != nil) != tt.wantErr {
  73. // t.Errorf("UpdateRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  74. // return
  75. // }
  76. // if !reflect.DeepEqual(got, tt.want) {
  77. // t.Errorf("UpdateRuleSet() got = %v, want %v", got, tt.want)
  78. // }
  79. // })
  80. // }
  81. // }
  82. //
  83. // func newUint32(a uint32) *uint32 {
  84. // return &a
  85. // }
  86. //
  87. // func newStr(a string) *string {
  88. // return &a
  89. // }
  90. var (
  91. apiKey = "2cc87f6213a4c40e5a90c8dced3c9c577eda5"
  92. apiEmail = "ll@centcloud.net"
  93. )
  94. //func TestUpdatePageRule(t *testing.T) {
  95. //
  96. // type args struct {
  97. // apiKey string
  98. // apiEmail string
  99. // zoneID string
  100. // rule *zone.Rule
  101. // }
  102. // tests := []struct {
  103. // name string
  104. // args args
  105. // want *cloudflare.PageRule
  106. // wantErr bool
  107. // }{
  108. // {
  109. // name: "测试1",
  110. // args: args{
  111. // apiKey: apiKey,
  112. // apiEmail: apiEmail,
  113. // zoneID: "e29551894461bf16e43a113e5ab09ed3",
  114. // rule: &zone.Rule{
  115. // Expression: "www.1011test5.com.cdn.sensordb.site/*\n",
  116. // Ttl: newUint32(18000),
  117. // QueryArgsType: newStr("ExcludeAll"),
  118. // QueryArgs: []string{"qwe12", "78u9hi"},
  119. // OriginHost: newStr("www.baidu.com"),
  120. // OriginPath: nil,
  121. // OriginProtocol: newUint32(80),
  122. // RequestHeader: nil,
  123. // ResponseHeader: nil,
  124. // },
  125. // },
  126. // want: nil,
  127. // wantErr: false,
  128. // },
  129. // // TODO: Add test cases.
  130. // }
  131. // for _, tt := range tests {
  132. // t.Run(tt.name, func(t *testing.T) {
  133. // got, err := UpdatePageRule(tt.args.apiKey, tt.args.apiEmail, tt.args.zoneID, tt.args.rule)
  134. // if (err != nil) != tt.wantErr {
  135. // t.Errorf("UpdatePageRule() error = %v, wantErr %v", err, tt.wantErr)
  136. // return
  137. // }
  138. // print("%v", got)
  139. // //if !reflect.DeepEqual(got, tt.want) {
  140. // // t.Errorf("UpdatePageRule() got = %v, want %v", got, tt.want)
  141. // //}
  142. // })
  143. // }
  144. //}
  145. func TestZoneServer_GetRuleSet(t *testing.T) {
  146. type fields struct {
  147. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  148. }
  149. type args struct {
  150. ctx context.Context
  151. requestCloudflare *zone.GetRuleSetRequestCloudflare
  152. }
  153. tests := []struct {
  154. name string
  155. fields fields
  156. args args
  157. want *zone.RuleList
  158. wantErr bool
  159. }{
  160. {
  161. name: "测试1",
  162. fields: fields{
  163. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  164. },
  165. args: args{
  166. ctx: context.Background(),
  167. requestCloudflare: &zone.GetRuleSetRequestCloudflare{
  168. ApiKey: apiKey,
  169. ApiEmail: apiEmail,
  170. ZoneId: "e29551894461bf16e43a113e5ab09ed3",
  171. Phase: zone.Phase_http_request_transform,
  172. },
  173. },
  174. want: nil,
  175. wantErr: false,
  176. },
  177. }
  178. for _, tt := range tests {
  179. t.Run(tt.name, func(t *testing.T) {
  180. s := &ZoneServer{
  181. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  182. }
  183. got, err := s.GetRuleSet(tt.args.ctx, tt.args.requestCloudflare)
  184. if (err != nil) != tt.wantErr {
  185. t.Errorf("GetRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  186. return
  187. }
  188. log.Print(got)
  189. //if !reflect.DeepEqual(got, tt.want) {
  190. // t.Errorf("GetRuleSet() got = %v, want %v", got, tt.want)
  191. //}
  192. })
  193. }
  194. }
  195. func TestZoneServer_UpdateRuleSet(t *testing.T) {
  196. type fields struct {
  197. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  198. }
  199. type args struct {
  200. ctx context.Context
  201. requestCloudflare *zone.UpdateRuleSetRequestCloudflare
  202. }
  203. tests := []struct {
  204. name string
  205. fields fields
  206. args args
  207. want *zone.RuleList
  208. wantErr bool
  209. }{
  210. {
  211. name: "测试1",
  212. fields: fields{
  213. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  214. },
  215. args: args{
  216. ctx: nil,
  217. requestCloudflare: &zone.UpdateRuleSetRequestCloudflare{
  218. ApiKey: apiKey,
  219. ApiEmail: apiEmail,
  220. ZoneId: "e29551894461bf16e43a113e5ab09ed3",
  221. Phase: zone.Phase_http_request_transform,
  222. Rules: []*zone.Rule{
  223. {
  224. Expression: "(http.request.uri.path contains \"www.1011test5.com.cdn.sensordb.site/\")",
  225. OriginPath: lo.ToPtr[string]("www.baidu.com"),
  226. RequestHeader: nil,
  227. ResponseHeader: nil,
  228. },
  229. },
  230. },
  231. },
  232. want: nil,
  233. wantErr: false,
  234. },
  235. }
  236. for _, tt := range tests {
  237. t.Run(tt.name, func(t *testing.T) {
  238. s := &ZoneServer{
  239. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  240. }
  241. got, err := s.UpdateRuleSet(tt.args.ctx, tt.args.requestCloudflare)
  242. if (err != nil) != tt.wantErr {
  243. t.Errorf("UpdateRuleSet() error = %v, wantErr %v", err, tt.wantErr)
  244. return
  245. }
  246. log.Print(got)
  247. //if !reflect.DeepEqual(got, tt.want) {
  248. // t.Errorf("UpdateRuleSet() got = %v, want %v", got, tt.want)
  249. //}
  250. })
  251. }
  252. }
  253. func TestZoneServer_GetArgoSetting(t *testing.T) {
  254. type fields struct {
  255. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  256. }
  257. type args struct {
  258. ctx context.Context
  259. requestCloudflare *zone.IdRequest
  260. }
  261. tests := []struct {
  262. name string
  263. fields fields
  264. args args
  265. want *zone.ArgoSetting
  266. wantErr bool
  267. }{
  268. {
  269. name: "test1",
  270. fields: fields{
  271. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  272. },
  273. args: args{
  274. ctx: context.Background(),
  275. requestCloudflare: &zone.IdRequest{
  276. ApiKey: apiKey,
  277. ApiEmail: apiEmail,
  278. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  279. },
  280. },
  281. want: &zone.ArgoSetting{Enabled: true},
  282. wantErr: false,
  283. },
  284. {
  285. name: "test1",
  286. fields: fields{
  287. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  288. },
  289. args: args{
  290. ctx: context.Background(),
  291. requestCloudflare: &zone.IdRequest{
  292. ApiKey: apiKey,
  293. ApiEmail: apiEmail,
  294. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  295. },
  296. },
  297. want: &zone.ArgoSetting{Enabled: false},
  298. wantErr: false,
  299. },
  300. }
  301. for _, tt := range tests {
  302. t.Run(tt.name, func(t *testing.T) {
  303. s := &ZoneServer{
  304. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  305. }
  306. got, err := s.GetArgoSetting(tt.args.ctx, tt.args.requestCloudflare)
  307. if (err != nil) != tt.wantErr {
  308. t.Errorf("GetArgoSetting() error = %v, wantErr %v", err, tt.wantErr)
  309. return
  310. }
  311. if !reflect.DeepEqual(got, tt.want) {
  312. t.Errorf("GetArgoSetting() got = %v, want %v", got, tt.want)
  313. }
  314. })
  315. }
  316. }
  317. func TestZoneServer_UpdateArgoSetting(t *testing.T) {
  318. type fields struct {
  319. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  320. }
  321. type args struct {
  322. ctx context.Context
  323. requestCloudflare *zone.UpdateArgoRequestCloudflare
  324. }
  325. tests := []struct {
  326. name string
  327. fields fields
  328. args args
  329. want *zone.ArgoSetting
  330. wantErr bool
  331. }{
  332. {
  333. name: "test1",
  334. fields: fields{
  335. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  336. },
  337. args: args{
  338. ctx: context.Background(),
  339. requestCloudflare: &zone.UpdateArgoRequestCloudflare{
  340. ApiKey: apiKey,
  341. ApiEmail: apiEmail,
  342. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  343. ArgoSetting: &zone.ArgoSetting{Enabled: false},
  344. },
  345. },
  346. want: &zone.ArgoSetting{Enabled: false},
  347. wantErr: false,
  348. },
  349. {
  350. name: "test1",
  351. fields: fields{
  352. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  353. },
  354. args: args{
  355. ctx: context.Background(),
  356. requestCloudflare: &zone.UpdateArgoRequestCloudflare{
  357. ApiKey: apiKey,
  358. ApiEmail: apiEmail,
  359. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  360. ArgoSetting: &zone.ArgoSetting{Enabled: true},
  361. },
  362. },
  363. want: &zone.ArgoSetting{Enabled: true},
  364. wantErr: false,
  365. },
  366. }
  367. for _, tt := range tests {
  368. t.Run(tt.name, func(t *testing.T) {
  369. s := &ZoneServer{
  370. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  371. }
  372. got, err := s.UpdateArgoSetting(tt.args.ctx, tt.args.requestCloudflare)
  373. if (err != nil) != tt.wantErr {
  374. t.Errorf("UpdateArgoSetting() error = %v, wantErr %v", err, tt.wantErr)
  375. return
  376. }
  377. if !reflect.DeepEqual(got, tt.want) {
  378. t.Errorf("UpdateArgoSetting() got = %v, want %v", got, tt.want)
  379. }
  380. })
  381. }
  382. }
  383. func TestZoneServer_GetArgoSettingSubscription(t *testing.T) {
  384. type fields struct {
  385. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  386. }
  387. type args struct {
  388. ctx context.Context
  389. request *zone.IdRequest
  390. }
  391. tests := []struct {
  392. name string
  393. fields fields
  394. args args
  395. want *zone.ArgoSetting
  396. wantErr bool
  397. }{
  398. {
  399. name: "启用测试",
  400. fields: fields{
  401. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  402. },
  403. args: args{
  404. ctx: nil,
  405. request: &zone.IdRequest{
  406. ApiKey: apiKey,
  407. ApiEmail: apiEmail,
  408. ZoneId: "f7eefa29114db32ab14397bdd5048ec8",
  409. },
  410. },
  411. want: &zone.ArgoSetting{Enabled: true},
  412. wantErr: false,
  413. },
  414. {
  415. name: "未启用测试",
  416. fields: fields{
  417. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  418. },
  419. args: args{
  420. ctx: nil,
  421. request: &zone.IdRequest{
  422. ApiKey: apiKey,
  423. ApiEmail: apiEmail,
  424. ZoneId: "6d89e53e52a08fc51b99628ff546c7fb",
  425. },
  426. },
  427. want: &zone.ArgoSetting{Enabled: false},
  428. wantErr: false,
  429. },
  430. }
  431. for _, tt := range tests {
  432. t.Run(tt.name, func(t *testing.T) {
  433. s := &ZoneServer{
  434. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  435. }
  436. got, err := s.GetArgoSettingSubscription(tt.args.ctx, tt.args.request)
  437. if (err != nil) != tt.wantErr {
  438. t.Errorf("GetArgoSettingSubscription() error = %v, wantErr %v", err, tt.wantErr)
  439. return
  440. }
  441. if !reflect.DeepEqual(got, tt.want) {
  442. t.Errorf("GetArgoSettingSubscription() got = %v, want %v", got, tt.want)
  443. }
  444. })
  445. }
  446. }
  447. func TestZoneServer_CreateArgoSettingSubscription(t *testing.T) {
  448. type fields struct {
  449. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  450. }
  451. type args struct {
  452. ctx context.Context
  453. request *zone.IdRequest
  454. }
  455. tests := []struct {
  456. name string
  457. fields fields
  458. args args
  459. want *zone.ArgoSetting
  460. wantErr bool
  461. }{
  462. {
  463. name: "测试",
  464. fields: fields{
  465. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  466. },
  467. args: args{
  468. ctx: nil,
  469. request: &zone.IdRequest{
  470. ApiKey: apiKey,
  471. ApiEmail: apiEmail,
  472. ZoneId: "7a25de4f4ea643e99c66e84e7498ccc6",
  473. },
  474. },
  475. want: &zone.ArgoSetting{Enabled: true},
  476. wantErr: false,
  477. },
  478. }
  479. for _, tt := range tests {
  480. t.Run(tt.name, func(t *testing.T) {
  481. s := &ZoneServer{
  482. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  483. }
  484. got, err := s.CreateArgoSettingSubscription(tt.args.ctx, tt.args.request)
  485. if (err != nil) != tt.wantErr {
  486. t.Errorf("CreateArgoSettingSubscription() error = %v, wantErr %v", err, tt.wantErr)
  487. return
  488. }
  489. if !reflect.DeepEqual(got, tt.want) {
  490. t.Errorf("CreateArgoSettingSubscription() got = %v, want %v", got, tt.want)
  491. }
  492. })
  493. }
  494. }
  495. func TestCreateZone(t *testing.T) {
  496. api, err := cloudflare.New("d2589c4f617b0493357ae187a7b4518171d07", "shiqi@centcloud.net")
  497. if err != nil {
  498. t.Fatal(err)
  499. }
  500. ctx := context.Background()
  501. account, r, err := api.Account(ctx, "b9145a83318cae896ad9b23bdf1681a5")
  502. if err != nil {
  503. t.Fatal(err)
  504. }
  505. t.Logf("Account Info:%+v", r)
  506. createZone, err := api.CreateZone(ctx, "cf004.shiqi.website", false, account, "partial")
  507. if err != nil {
  508. t.Fatal(err)
  509. }
  510. t.Logf("createZone: %+v", createZone)
  511. time.Sleep(time.Second)
  512. zoneId := createZone.ID
  513. err = api.ZoneSetPlan(ctx, zoneId, "PARTNERS_ENT")
  514. if err != nil {
  515. t.Fatal(err)
  516. }
  517. }
  518. func TestZoneServer_CreateZone(t *testing.T) {
  519. type fields struct {
  520. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  521. }
  522. type args struct {
  523. ctx context.Context
  524. requestCloudflare *zone.CreateZoneRequestCloudflare
  525. }
  526. tests := []struct {
  527. name string
  528. fields fields
  529. args args
  530. want *zone.CreateZoneReplyCloudflare
  531. wantErr bool
  532. }{
  533. {
  534. name: "测试用例1",
  535. fields: fields{
  536. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  537. },
  538. args: args{
  539. ctx: context.Background(),
  540. requestCloudflare: &zone.CreateZoneRequestCloudflare{
  541. ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
  542. ApiEmail: "shiqi@centcloud.net",
  543. AccountId: "b9145a83318cae896ad9b23bdf1681a5",
  544. Domain: "cf005.shiqi.website",
  545. },
  546. },
  547. want: nil,
  548. wantErr: false,
  549. },
  550. }
  551. for _, tt := range tests {
  552. t.Run(tt.name, func(t *testing.T) {
  553. s := &ZoneServer{
  554. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  555. }
  556. got, err := s.CreateZone(tt.args.ctx, tt.args.requestCloudflare)
  557. if (err != nil) != tt.wantErr {
  558. t.Errorf("CreateZone() error = %v, wantErr %v", err, tt.wantErr)
  559. return
  560. }
  561. t.Logf("CreateZone() got = %v", got)
  562. })
  563. }
  564. }
  565. func TestZoneServer_SetZonePlan(t *testing.T) {
  566. type fields struct {
  567. UnimplementedZoneCloudflareServer zone.UnimplementedZoneCloudflareServer
  568. }
  569. type args struct {
  570. ctx context.Context
  571. requestCloudflare *zone.SetZonePlanRequestCloudflare
  572. }
  573. tests := []struct {
  574. name string
  575. fields fields
  576. args args
  577. want *zone.SetZonePlanReplyCloudflare
  578. wantErr bool
  579. }{
  580. {
  581. name: "测试用例1",
  582. fields: fields{
  583. UnimplementedZoneCloudflareServer: zone.UnimplementedZoneCloudflareServer{},
  584. },
  585. args: args{
  586. ctx: context.Background(),
  587. requestCloudflare: &zone.SetZonePlanRequestCloudflare{
  588. ApiKey: "d2589c4f617b0493357ae187a7b4518171d07",
  589. ApiEmail: "shiqi@centcloud.net",
  590. ZoneId: "19e7978bb9292fe11d3c75ff39f87685",
  591. ZonePlan: "PARTNERS_ENT",
  592. },
  593. },
  594. want: &zone.SetZonePlanReplyCloudflare{Success: true},
  595. wantErr: false,
  596. },
  597. }
  598. for _, tt := range tests {
  599. t.Run(tt.name, func(t *testing.T) {
  600. s := &ZoneServer{
  601. UnimplementedZoneCloudflareServer: tt.fields.UnimplementedZoneCloudflareServer,
  602. }
  603. got, err := s.SetZonePlan(tt.args.ctx, tt.args.requestCloudflare)
  604. if (err != nil) != tt.wantErr {
  605. t.Errorf("SetZonePlan() error = %v, wantErr %v", err, tt.wantErr)
  606. return
  607. }
  608. if !reflect.DeepEqual(got, tt.want) {
  609. t.Errorf("SetZonePlan() got = %v, want %v", got, tt.want)
  610. }
  611. })
  612. }
  613. }