order_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/x509"
  6. "encoding/base64"
  7. "encoding/json"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http/httptest"
  11. "net/url"
  12. "testing"
  13. "time"
  14. "github.com/go-chi/chi"
  15. "github.com/smallstep/assert"
  16. "github.com/smallstep/certificates/acme"
  17. "go.step.sm/crypto/pemutil"
  18. )
  19. func TestNewOrderRequestValidate(t *testing.T) {
  20. type test struct {
  21. nor *NewOrderRequest
  22. nbf, naf time.Time
  23. err *acme.Error
  24. }
  25. var tests = map[string]func(t *testing.T) test{
  26. "fail/no-identifiers": func(t *testing.T) test {
  27. return test{
  28. nor: &NewOrderRequest{},
  29. err: acme.NewError(acme.ErrorMalformedType, "identifiers list cannot be empty"),
  30. }
  31. },
  32. "fail/bad-identifier": func(t *testing.T) test {
  33. return test{
  34. nor: &NewOrderRequest{
  35. Identifiers: []acme.Identifier{
  36. {Type: "dns", Value: "example.com"},
  37. {Type: "foo", Value: "bar.com"},
  38. },
  39. },
  40. err: acme.NewError(acme.ErrorMalformedType, "identifier type unsupported: foo"),
  41. }
  42. },
  43. "ok": func(t *testing.T) test {
  44. nbf := time.Now().UTC().Add(time.Minute)
  45. naf := time.Now().UTC().Add(5 * time.Minute)
  46. return test{
  47. nor: &NewOrderRequest{
  48. Identifiers: []acme.Identifier{
  49. {Type: "dns", Value: "example.com"},
  50. {Type: "dns", Value: "bar.com"},
  51. },
  52. NotAfter: naf,
  53. NotBefore: nbf,
  54. },
  55. nbf: nbf,
  56. naf: naf,
  57. }
  58. },
  59. }
  60. for name, run := range tests {
  61. tc := run(t)
  62. t.Run(name, func(t *testing.T) {
  63. if err := tc.nor.Validate(); err != nil {
  64. if assert.NotNil(t, err) {
  65. ae, ok := err.(*acme.Error)
  66. assert.True(t, ok)
  67. assert.HasPrefix(t, ae.Error(), tc.err.Error())
  68. assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
  69. assert.Equals(t, ae.Type, tc.err.Type)
  70. }
  71. } else {
  72. if assert.Nil(t, tc.err) {
  73. if tc.nbf.IsZero() {
  74. assert.True(t, tc.nor.NotBefore.Before(time.Now().Add(time.Minute)))
  75. assert.True(t, tc.nor.NotBefore.After(time.Now().Add(-time.Minute)))
  76. } else {
  77. assert.Equals(t, tc.nor.NotBefore, tc.nbf)
  78. }
  79. if tc.naf.IsZero() {
  80. assert.True(t, tc.nor.NotAfter.Before(time.Now().Add(24*time.Hour)))
  81. assert.True(t, tc.nor.NotAfter.After(time.Now().Add(24*time.Hour-time.Minute)))
  82. } else {
  83. assert.Equals(t, tc.nor.NotAfter, tc.naf)
  84. }
  85. }
  86. }
  87. })
  88. }
  89. }
  90. func TestFinalizeRequestValidate(t *testing.T) {
  91. _csr, err := pemutil.Read("../../authority/testdata/certs/foo.csr")
  92. assert.FatalError(t, err)
  93. csr, ok := _csr.(*x509.CertificateRequest)
  94. assert.Fatal(t, ok)
  95. type test struct {
  96. fr *FinalizeRequest
  97. err *acme.Error
  98. }
  99. var tests = map[string]func(t *testing.T) test{
  100. "fail/parse-csr-error": func(t *testing.T) test {
  101. return test{
  102. fr: &FinalizeRequest{},
  103. err: acme.NewError(acme.ErrorMalformedType, "unable to parse csr: asn1: syntax error: sequence truncated"),
  104. }
  105. },
  106. "fail/invalid-csr-signature": func(t *testing.T) test {
  107. b, err := pemutil.Read("../../authority/testdata/certs/badsig.csr")
  108. assert.FatalError(t, err)
  109. c, ok := b.(*x509.CertificateRequest)
  110. assert.Fatal(t, ok)
  111. return test{
  112. fr: &FinalizeRequest{
  113. CSR: base64.RawURLEncoding.EncodeToString(c.Raw),
  114. },
  115. err: acme.NewError(acme.ErrorMalformedType, "csr failed signature check: x509: ECDSA verification failure"),
  116. }
  117. },
  118. "ok": func(t *testing.T) test {
  119. return test{
  120. fr: &FinalizeRequest{
  121. CSR: base64.RawURLEncoding.EncodeToString(csr.Raw),
  122. },
  123. }
  124. },
  125. }
  126. for name, run := range tests {
  127. tc := run(t)
  128. t.Run(name, func(t *testing.T) {
  129. if err := tc.fr.Validate(); err != nil {
  130. if assert.NotNil(t, err) {
  131. ae, ok := err.(*acme.Error)
  132. assert.True(t, ok)
  133. assert.HasPrefix(t, ae.Error(), tc.err.Error())
  134. assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
  135. assert.Equals(t, ae.Type, tc.err.Type)
  136. }
  137. } else {
  138. if assert.Nil(t, tc.err) {
  139. assert.Equals(t, tc.fr.csr.Raw, csr.Raw)
  140. }
  141. }
  142. })
  143. }
  144. }
  145. func TestHandler_GetOrder(t *testing.T) {
  146. expiry := time.Now().UTC().Add(6 * time.Hour)
  147. nbf := time.Now().UTC()
  148. naf := time.Now().UTC().Add(24 * time.Hour)
  149. o := acme.Order{
  150. ID: "orderID",
  151. Expires: expiry,
  152. NotBefore: nbf,
  153. NotAfter: naf,
  154. Identifiers: []acme.Identifier{
  155. {
  156. Type: "dns",
  157. Value: "example.com",
  158. },
  159. {
  160. Type: "dns",
  161. Value: "*.smallstep.com",
  162. },
  163. },
  164. Status: "pending",
  165. AuthorizationURLs: []string{"foo", "bar"},
  166. }
  167. // Request with chi context
  168. chiCtx := chi.NewRouteContext()
  169. chiCtx.URLParams.Add("ordID", o.ID)
  170. prov := newProv()
  171. provName := url.PathEscape(prov.GetName())
  172. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  173. url := fmt.Sprintf("%s/acme/%s/order/%s",
  174. baseURL.String(), provName, o.ID)
  175. type test struct {
  176. db acme.DB
  177. linker Linker
  178. ctx context.Context
  179. statusCode int
  180. err *acme.Error
  181. }
  182. var tests = map[string]func(t *testing.T) test{
  183. "fail/no-account": func(t *testing.T) test {
  184. return test{
  185. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  186. statusCode: 400,
  187. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  188. }
  189. },
  190. "fail/nil-account": func(t *testing.T) test {
  191. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  192. ctx = context.WithValue(ctx, accContextKey, nil)
  193. return test{
  194. ctx: ctx,
  195. statusCode: 400,
  196. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  197. }
  198. },
  199. "fail/getOrder-error": func(t *testing.T) test {
  200. acc := &acme.Account{ID: "accID"}
  201. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  202. ctx = context.WithValue(ctx, accContextKey, acc)
  203. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  204. return test{
  205. db: &acme.MockDB{
  206. MockError: acme.NewErrorISE("force"),
  207. },
  208. ctx: ctx,
  209. statusCode: 500,
  210. err: acme.NewErrorISE("force"),
  211. }
  212. },
  213. "ok": func(t *testing.T) test {
  214. acc := &acme.Account{ID: "accID"}
  215. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  216. ctx = context.WithValue(ctx, accContextKey, acc)
  217. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  218. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  219. return test{
  220. db: &acme.MockDB{
  221. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  222. assert.Equals(t, id, o.ID)
  223. return &o, nil
  224. },
  225. },
  226. linker: NewLinker("dns", "acme"),
  227. ctx: ctx,
  228. statusCode: 200,
  229. }
  230. },
  231. }
  232. for name, run := range tests {
  233. tc := run(t)
  234. t.Run(name, func(t *testing.T) {
  235. h := &Handler{linker: tc.linker, db: tc.db}
  236. req := httptest.NewRequest("GET", url, nil)
  237. req = req.WithContext(tc.ctx)
  238. w := httptest.NewRecorder()
  239. h.GetOrder(w, req)
  240. res := w.Result()
  241. assert.Equals(t, res.StatusCode, tc.statusCode)
  242. body, err := ioutil.ReadAll(res.Body)
  243. res.Body.Close()
  244. assert.FatalError(t, err)
  245. if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
  246. var ae acme.Error
  247. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  248. assert.Equals(t, ae.Type, tc.err.Type)
  249. assert.Equals(t, ae.Detail, tc.err.Detail)
  250. assert.Equals(t, ae.Identifier, tc.err.Identifier)
  251. assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
  252. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  253. } else {
  254. expB, err := json.Marshal(o)
  255. assert.FatalError(t, err)
  256. assert.Equals(t, bytes.TrimSpace(body), expB)
  257. assert.Equals(t, res.Header["Location"], []string{url})
  258. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  259. }
  260. })
  261. }
  262. }
  263. func TestHandlerNewOrder(t *testing.T) {
  264. expiry := time.Now().UTC().Add(6 * time.Hour)
  265. nbf := time.Now().UTC().Add(5 * time.Hour)
  266. naf := nbf.Add(17 * time.Hour)
  267. o := acme.Order{
  268. ID: "orderID",
  269. Expires: expiry,
  270. NotBefore: nbf,
  271. NotAfter: naf,
  272. Identifiers: []acme.Identifier{
  273. {Type: "dns", Value: "example.com"},
  274. {Type: "dns", Value: "bar.com"},
  275. },
  276. Status: "pending",
  277. AuthorizationURLs: []string{"foo", "bar"},
  278. }
  279. prov := newProv()
  280. provName := url.PathEscape(prov.GetName())
  281. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  282. url := fmt.Sprintf("%s/acme/%s/new-order",
  283. baseURL.String(), provName)
  284. type test struct {
  285. db acme.DB
  286. linker Linker
  287. ctx context.Context
  288. statusCode int
  289. err *acme.Error
  290. }
  291. var tests = map[string]func(t *testing.T) test{
  292. "fail/no-account": func(t *testing.T) test {
  293. return test{
  294. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  295. statusCode: 400,
  296. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  297. }
  298. },
  299. "fail/nil-account": func(t *testing.T) test {
  300. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  301. ctx = context.WithValue(ctx, accContextKey, nil)
  302. return test{
  303. ctx: ctx,
  304. statusCode: 400,
  305. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  306. }
  307. },
  308. "fail/no-payload": func(t *testing.T) test {
  309. acc := &acme.Account{ID: "accID"}
  310. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  311. ctx = context.WithValue(ctx, accContextKey, acc)
  312. return test{
  313. ctx: ctx,
  314. statusCode: 500,
  315. err: acme.NewErrorISE("payload expected in request context"),
  316. }
  317. },
  318. "fail/nil-payload": func(t *testing.T) test {
  319. acc := &acme.Account{ID: "accID"}
  320. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  321. ctx = context.WithValue(ctx, accContextKey, acc)
  322. ctx = context.WithValue(ctx, payloadContextKey, nil)
  323. return test{
  324. ctx: ctx,
  325. statusCode: 500,
  326. err: acme.NewErrorISE("payload expected in request context"),
  327. }
  328. },
  329. "fail/unmarshal-payload-error": func(t *testing.T) test {
  330. acc := &acme.Account{ID: "accID"}
  331. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  332. ctx = context.WithValue(ctx, accContextKey, acc)
  333. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
  334. return test{
  335. ctx: ctx,
  336. statusCode: 400,
  337. err: acme.NewError(acme.ErrorMalformedType, "failed to unmarshal new-order request payload: unexpected end of JSON input"),
  338. }
  339. },
  340. "fail/malformed-payload-error": func(t *testing.T) test {
  341. acc := &acme.Account{ID: "accID"}
  342. nor := &NewOrderRequest{}
  343. b, err := json.Marshal(nor)
  344. assert.FatalError(t, err)
  345. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  346. ctx = context.WithValue(ctx, accContextKey, acc)
  347. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  348. return test{
  349. ctx: ctx,
  350. statusCode: 400,
  351. err: acme.NewError(acme.ErrorMalformedType, "identifiers list cannot be empty"),
  352. }
  353. },
  354. "fail/NewOrder-error": func(t *testing.T) test {
  355. acc := &acme.Account{ID: "accID"}
  356. nor := &NewOrderRequest{
  357. Identifiers: []acme.Identifier{
  358. {Type: "dns", Value: "example.com"},
  359. {Type: "dns", Value: "bar.com"},
  360. },
  361. }
  362. b, err := json.Marshal(nor)
  363. assert.FatalError(t, err)
  364. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  365. ctx = context.WithValue(ctx, accContextKey, acc)
  366. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  367. return test{
  368. db: &acme.MockDB{
  369. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  370. return acme.NewError(acme.ErrorMalformedType, "force")
  371. },
  372. },
  373. ctx: ctx,
  374. statusCode: 400,
  375. err: acme.NewError(acme.ErrorMalformedType, "force"),
  376. }
  377. },
  378. "ok": func(t *testing.T) test {
  379. acc := &acme.Account{ID: "accID"}
  380. nor := &NewOrderRequest{
  381. Identifiers: []acme.Identifier{
  382. {Type: "dns", Value: "example.com"},
  383. {Type: "dns", Value: "bar.com"},
  384. },
  385. NotBefore: nbf,
  386. NotAfter: naf,
  387. }
  388. b, err := json.Marshal(nor)
  389. assert.FatalError(t, err)
  390. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  391. ctx = context.WithValue(ctx, accContextKey, acc)
  392. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  393. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  394. return test{
  395. db: &acme.MockDB{
  396. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  397. return nil
  398. },
  399. },
  400. linker: NewLinker("dns", "acme"),
  401. ctx: ctx,
  402. statusCode: 201,
  403. }
  404. },
  405. "ok/default-naf-nbf": func(t *testing.T) test {
  406. acc := &acme.Account{ID: "accID"}
  407. nor := &NewOrderRequest{
  408. Identifiers: []acme.Identifier{
  409. {Type: "dns", Value: "example.com"},
  410. {Type: "dns", Value: "bar.com"},
  411. },
  412. }
  413. b, err := json.Marshal(nor)
  414. assert.FatalError(t, err)
  415. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  416. ctx = context.WithValue(ctx, accContextKey, acc)
  417. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  418. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  419. return test{
  420. db: &acme.MockDB{
  421. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  422. return nil
  423. },
  424. },
  425. linker: NewLinker("dns", "acme"),
  426. ctx: ctx,
  427. statusCode: 201,
  428. }
  429. },
  430. }
  431. for name, run := range tests {
  432. tc := run(t)
  433. t.Run(name, func(t *testing.T) {
  434. h := &Handler{linker: tc.linker, db: tc.db}
  435. req := httptest.NewRequest("GET", url, nil)
  436. req = req.WithContext(tc.ctx)
  437. w := httptest.NewRecorder()
  438. h.NewOrder(w, req)
  439. res := w.Result()
  440. assert.Equals(t, res.StatusCode, tc.statusCode)
  441. body, err := ioutil.ReadAll(res.Body)
  442. res.Body.Close()
  443. assert.FatalError(t, err)
  444. if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
  445. var ae acme.Error
  446. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  447. assert.Equals(t, ae.Type, tc.err.Type)
  448. assert.Equals(t, ae.Detail, tc.err.Detail)
  449. assert.Equals(t, ae.Identifier, tc.err.Identifier)
  450. assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
  451. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  452. } else {
  453. expB, err := json.Marshal(o)
  454. assert.FatalError(t, err)
  455. assert.Equals(t, bytes.TrimSpace(body), expB)
  456. assert.Equals(t, res.Header["Location"],
  457. []string{fmt.Sprintf("%s/acme/%s/order/%s", baseURL.String(),
  458. provName, o.ID)})
  459. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  460. }
  461. })
  462. }
  463. }
  464. func TestHandler_FinalizeOrder(t *testing.T) {
  465. expiry := time.Now().UTC().Add(6 * time.Hour)
  466. nbf := time.Now().UTC().Add(5 * time.Hour)
  467. naf := nbf.Add(17 * time.Hour)
  468. o := acme.Order{
  469. ID: "orderID",
  470. Expires: expiry,
  471. NotBefore: nbf,
  472. NotAfter: naf,
  473. Identifiers: []acme.Identifier{
  474. {Type: "dns", Value: "example.com"},
  475. {Type: "dns", Value: "bar.com"},
  476. },
  477. Status: "valid",
  478. AuthorizationURLs: []string{"foo", "bar"},
  479. CertificateURL: "https://ca.smallstep.com/acme/certificate/certID",
  480. }
  481. _csr, err := pemutil.Read("../../authority/testdata/certs/foo.csr")
  482. assert.FatalError(t, err)
  483. csr, ok := _csr.(*x509.CertificateRequest)
  484. assert.Fatal(t, ok)
  485. // Request with chi context
  486. chiCtx := chi.NewRouteContext()
  487. chiCtx.URLParams.Add("ordID", o.ID)
  488. prov := newProv()
  489. provName := url.PathEscape(prov.GetName())
  490. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  491. url := fmt.Sprintf("%s/acme/%s/order/%s/finalize",
  492. baseURL.String(), provName, o.ID)
  493. type test struct {
  494. db acme.DB
  495. linker Linker
  496. ctx context.Context
  497. statusCode int
  498. err *acme.Error
  499. }
  500. var tests = map[string]func(t *testing.T) test{
  501. "fail/no-account": func(t *testing.T) test {
  502. return test{
  503. db: &acme.MockDB{},
  504. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  505. statusCode: 400,
  506. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  507. }
  508. },
  509. "fail/nil-account": func(t *testing.T) test {
  510. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  511. ctx = context.WithValue(ctx, accContextKey, nil)
  512. return test{
  513. db: &acme.MockDB{},
  514. ctx: ctx,
  515. statusCode: 400,
  516. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  517. }
  518. },
  519. "fail/no-payload": func(t *testing.T) test {
  520. acc := &acme.Account{ID: "accID"}
  521. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  522. ctx = context.WithValue(ctx, accContextKey, acc)
  523. return test{
  524. ctx: ctx,
  525. statusCode: 500,
  526. err: acme.NewErrorISE("payload expected in request context"),
  527. }
  528. },
  529. "fail/nil-payload": func(t *testing.T) test {
  530. acc := &acme.Account{ID: "accID"}
  531. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  532. ctx = context.WithValue(ctx, accContextKey, acc)
  533. ctx = context.WithValue(ctx, payloadContextKey, nil)
  534. return test{
  535. ctx: ctx,
  536. statusCode: 500,
  537. err: acme.NewErrorISE("payload expected in request context"),
  538. }
  539. },
  540. "fail/unmarshal-payload-error": func(t *testing.T) test {
  541. acc := &acme.Account{ID: "accID"}
  542. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  543. ctx = context.WithValue(ctx, accContextKey, acc)
  544. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
  545. return test{
  546. ctx: ctx,
  547. statusCode: 400,
  548. err: acme.NewError(acme.ErrorMalformedType, "failed to unmarshal finalize-order request payload: unexpected end of JSON input"),
  549. }
  550. },
  551. "fail/malformed-payload-error": func(t *testing.T) test {
  552. acc := &acme.Account{ID: "accID"}
  553. fr := &FinalizeRequest{}
  554. b, err := json.Marshal(fr)
  555. assert.FatalError(t, err)
  556. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  557. ctx = context.WithValue(ctx, accContextKey, acc)
  558. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  559. return test{
  560. ctx: ctx,
  561. statusCode: 400,
  562. err: acme.NewError(acme.ErrorMalformedType, "unable to parse csr: asn1: syntax error: sequence truncated"),
  563. }
  564. },
  565. "fail/FinalizeOrder-error": func(t *testing.T) test {
  566. acc := &acme.Account{ID: "accID"}
  567. nor := &FinalizeRequest{
  568. CSR: base64.RawURLEncoding.EncodeToString(csr.Raw),
  569. }
  570. b, err := json.Marshal(nor)
  571. assert.FatalError(t, err)
  572. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  573. ctx = context.WithValue(ctx, accContextKey, acc)
  574. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  575. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  576. return test{
  577. db: &acme.MockDB{
  578. MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
  579. /*
  580. p, err := acme.ProvisionerFromContext(ctx)
  581. assert.FatalError(t, err)
  582. assert.Equals(t, p, prov)
  583. assert.Equals(t, accID, acc.ID)
  584. assert.Equals(t, id, o.ID)
  585. assert.Equals(t, incsr.Raw, csr.Raw)
  586. */
  587. return acme.NewError(acme.ErrorMalformedType, "force")
  588. },
  589. },
  590. ctx: ctx,
  591. statusCode: 400,
  592. err: acme.NewError(acme.ErrorMalformedType, "force"),
  593. }
  594. },
  595. "ok": func(t *testing.T) test {
  596. acc := &acme.Account{ID: "accID"}
  597. nor := &FinalizeRequest{
  598. CSR: base64.RawURLEncoding.EncodeToString(csr.Raw),
  599. }
  600. b, err := json.Marshal(nor)
  601. assert.FatalError(t, err)
  602. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  603. ctx = context.WithValue(ctx, accContextKey, acc)
  604. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  605. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  606. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  607. return test{
  608. linker: NewLinker("dns", "acme"),
  609. db: &acme.MockDB{
  610. MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
  611. /*
  612. p, err := acme.ProvisionerFromContext(ctx)
  613. assert.FatalError(t, err)
  614. assert.Equals(t, p, prov)
  615. assert.Equals(t, accID, acc.ID)
  616. assert.Equals(t, id, o.ID)
  617. assert.Equals(t, incsr.Raw, csr.Raw)
  618. return &o, nil
  619. */
  620. return nil
  621. },
  622. },
  623. ctx: ctx,
  624. statusCode: 200,
  625. }
  626. },
  627. }
  628. for name, run := range tests {
  629. tc := run(t)
  630. t.Run(name, func(t *testing.T) {
  631. h := &Handler{linker: tc.linker, db: tc.db}
  632. req := httptest.NewRequest("GET", url, nil)
  633. req = req.WithContext(tc.ctx)
  634. w := httptest.NewRecorder()
  635. h.FinalizeOrder(w, req)
  636. res := w.Result()
  637. assert.Equals(t, res.StatusCode, tc.statusCode)
  638. body, err := ioutil.ReadAll(res.Body)
  639. res.Body.Close()
  640. assert.FatalError(t, err)
  641. if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
  642. var ae acme.Error
  643. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  644. assert.Equals(t, ae.Type, tc.err.Type)
  645. assert.Equals(t, ae.Detail, tc.err.Detail)
  646. assert.Equals(t, ae.Identifier, tc.err.Identifier)
  647. assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
  648. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  649. } else {
  650. expB, err := json.Marshal(o)
  651. assert.FatalError(t, err)
  652. assert.Equals(t, bytes.TrimSpace(body), expB)
  653. assert.Equals(t, res.Header["Location"],
  654. []string{fmt.Sprintf("%s/acme/%s/order/%s",
  655. baseURL, provName, o.ID)})
  656. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  657. }
  658. })
  659. }
  660. }