Debian Codegraph @

pkg/apis/dpf/v1/contracts/tsig_common_config_test.go

raw
1 package contracts_test
2
3 import (
4 "context"
5 "net/http"
6
7 . "github.com/onsi/ginkgo"
8 . "github.com/onsi/gomega"
9
10 "github.com/jarcoal/httpmock"
11 "github.com/mimuret/golang-iij-dpf/pkg/apis/dpf/v1/contracts"
12 "github.com/mimuret/golang-iij-dpf/pkg/testtool"
13 "github.com/mimuret/golang-iij-dpf/pkg/types"
14 )
15
16 var _ = Describe("tsigs/common_configs", func() {
17 var (
18 c contracts.TsigCommonConfigList
19 cl *testtool.TestClient
20 err error
21 reqId string
22 s1, s2 contracts.CommonConfig
23 slist contracts.TsigCommonConfigList
24 )
25 BeforeEach(func() {
26 cl = testtool.NewTestClient("", "http://localhost", nil)
27 s1 = contracts.CommonConfig{
28 AttributeMeta: contracts.AttributeMeta{
29 ContractID: "f1",
30 },
31 ID: 1,
32 Name: "共通設定1",
33 ManagedDNSEnabled: types.Enabled,
34 Default: types.Enabled,
35 Description: "common config 1",
36 }
37 s2 = contracts.CommonConfig{
38 AttributeMeta: contracts.AttributeMeta{
39 ContractID: "f1",
40 },
41 ID: 2,
42 Name: "共通設定2",
43 ManagedDNSEnabled: types.Disabled,
44 Default: types.Disabled,
45 Description: "common config 2",
46 }
47 slist = contracts.TsigCommonConfigList{
48 AttributeMeta: contracts.AttributeMeta{
49 ContractID: "f1",
50 },
51 Items: []contracts.CommonConfig{s1, s2},
52 ID: 1,
53 }
54 })
55 Describe("TsigCommonConfigList", func() {
56 Context("List", func() {
57 BeforeEach(func() {
58 httpmock.RegisterResponder(http.MethodGet, "http://localhost/contracts/f1/tsigs/1/common_configs", httpmock.NewBytesResponder(200, []byte(`{
59 "request_id": "C38FBA61C657432695BC798DD96B0D9A",
60 "results": [
61 {
62 "id": 1,
63 "name": "共通設定1",
64 "managed_dns_enabled": 1,
65 "default": 1,
66 "description": "common config 1"
67 },
68 {
69 "id": 2,
70 "name": "共通設定2",
71 "managed_dns_enabled": 0,
72 "default": 0,
73 "description": "common config 2"
74 }
75 ]
76 }`)))
77 httpmock.RegisterResponder(http.MethodGet, "http://localhost/contracts/f1/tsigs/1/common_configs/count", httpmock.NewBytesResponder(200, []byte(`{
78 "request_id": "1073C4F38C0A4027B4776F0F3E8197BB",
79 "result": {
80 "count": 2
81 }
82 }`)))
83 })
84 AfterEach(func() {
85 httpmock.Reset()
86 })
87 When("returns list ", func() {
88 BeforeEach(func() {
89 c = contracts.TsigCommonConfigList{
90 AttributeMeta: contracts.AttributeMeta{
91 ContractID: "f1",
92 },
93 ID: 1,
94 }
95 reqId, err = cl.List(context.Background(), &c, nil)
96 })
97 It("returns normal", func() {
98 Expect(err).To(Succeed())
99 Expect(reqId).To(Equal("C38FBA61C657432695BC798DD96B0D9A"))
100 Expect(c).To(Equal(slist))
101 })
102 })
103 })
104 Context("SetPathParams", func() {
105 When("no arguments, nothing to do", func() {
106 BeforeEach(func() {
107 err = slist.SetPathParams()
108 })
109 It("returns error", func() {
110 Expect(err).To(Succeed())
111 })
112 })
113 When("enough arguments", func() {
114 BeforeEach(func() {
115 err = slist.SetPathParams("id12", 122)
116 })
117 It("not returns error", func() {
118 Expect(err).To(Succeed())
119 })
120 It("can set ContractID", func() {
121 Expect(slist.GetContractID()).To(Equal("id12"))
122 })
123 It("can set Id (tsig_id)", func() {
124 Expect(slist.ID).To(Equal(int64(122)))
125 })
126 })
127 When("not enough arguments", func() {
128 BeforeEach(func() {
129 err = slist.SetPathParams("id12")
130 })
131 It("not returns error", func() {
132 Expect(err).To(HaveOccurred())
133 })
134 })
135 When("arguments has extra value", func() {
136 BeforeEach(func() {
137 err = slist.SetPathParams("f1", 2, 3)
138 })
139 It("returns error", func() {
140 Expect(err).To(HaveOccurred())
141 })
142 })
143 When("arguments type missmatch (ContractID)", func() {
144 BeforeEach(func() {
145 err = slist.SetPathParams(2, 2)
146 })
147 It("returns error", func() {
148 Expect(err).To(HaveOccurred())
149 })
150 })
151 When("arguments type missmatch (Id)", func() {
152 BeforeEach(func() {
153 err = slist.SetPathParams("F1", "f2")
154 })
155 It("returns error", func() {
156 Expect(err).To(HaveOccurred())
157 })
158 })
159 })
160 Context("api.Spec common test", func() {
161 var nilSpec *contracts.TsigCommonConfigList
162 testtool.TestDeepCopyObject(&slist, nilSpec)
163 testtool.TestGetName(&slist, "tsigs/1/common_configs")
164
165 testtool.TestGetPathMethodForCountableList(&slist, "/contracts/f1/tsigs/1/common_configs")
166 })
167 Context("api.ListSpec common test", func() {
168 testtool.TestGetItems(&slist, &slist.Items)
169 testtool.TestLen(&slist, 2)
170 Context("Index", func() {
171 When("index exist", func() {
172 It("returns item", func() {
173 Expect(slist.Index(0)).To(Equal(s1))
174 })
175 })
176 })
177 })
178 Context("api.CountableListSpec common test", func() {
179 testtool.TestGetMaxLimit(&slist, 10000)
180 testtool.TestClearItems(&slist)
181 testtool.TestAddItem(&slist, s1)
182 })
183 Context("contracts.ChildSpec common test", func() {
184 Context("GetID", func() {
185 It("returns Id", func() {
186 Expect(slist.GetID()).To(Equal(slist.ID))
187 })
188 })
189 Context("SetID", func() {
190 BeforeEach(func() {
191 slist.SetID(2)
192 })
193 It("can set Id", func() {
194 Expect(slist.GetID()).To(Equal(int64(2)))
195 })
196 })
197 })
198 })
199 })