order_test.go 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  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/pkg/errors"
  16. "github.com/smallstep/assert"
  17. "github.com/smallstep/certificates/acme"
  18. "go.step.sm/crypto/pemutil"
  19. )
  20. func TestNewOrderRequest_Validate(t *testing.T) {
  21. type test struct {
  22. nor *NewOrderRequest
  23. nbf, naf time.Time
  24. err *acme.Error
  25. }
  26. var tests = map[string]func(t *testing.T) test{
  27. "fail/no-identifiers": func(t *testing.T) test {
  28. return test{
  29. nor: &NewOrderRequest{},
  30. err: acme.NewError(acme.ErrorMalformedType, "identifiers list cannot be empty"),
  31. }
  32. },
  33. "fail/bad-identifier": func(t *testing.T) test {
  34. return test{
  35. nor: &NewOrderRequest{
  36. Identifiers: []acme.Identifier{
  37. {Type: "dns", Value: "example.com"},
  38. {Type: "foo", Value: "bar.com"},
  39. },
  40. },
  41. err: acme.NewError(acme.ErrorMalformedType, "identifier type unsupported: foo"),
  42. }
  43. },
  44. "ok": func(t *testing.T) test {
  45. nbf := time.Now().UTC().Add(time.Minute)
  46. naf := time.Now().UTC().Add(5 * time.Minute)
  47. return test{
  48. nor: &NewOrderRequest{
  49. Identifiers: []acme.Identifier{
  50. {Type: "dns", Value: "example.com"},
  51. {Type: "dns", Value: "bar.com"},
  52. },
  53. NotAfter: naf,
  54. NotBefore: nbf,
  55. },
  56. nbf: nbf,
  57. naf: naf,
  58. }
  59. },
  60. }
  61. for name, run := range tests {
  62. tc := run(t)
  63. t.Run(name, func(t *testing.T) {
  64. if err := tc.nor.Validate(); err != nil {
  65. if assert.NotNil(t, err) {
  66. ae, ok := err.(*acme.Error)
  67. assert.True(t, ok)
  68. assert.HasPrefix(t, ae.Error(), tc.err.Error())
  69. assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
  70. assert.Equals(t, ae.Type, tc.err.Type)
  71. }
  72. } else {
  73. if assert.Nil(t, tc.err) {
  74. if tc.nbf.IsZero() {
  75. assert.True(t, tc.nor.NotBefore.Before(time.Now().Add(time.Minute)))
  76. assert.True(t, tc.nor.NotBefore.After(time.Now().Add(-time.Minute)))
  77. } else {
  78. assert.Equals(t, tc.nor.NotBefore, tc.nbf)
  79. }
  80. if tc.naf.IsZero() {
  81. assert.True(t, tc.nor.NotAfter.Before(time.Now().Add(24*time.Hour)))
  82. assert.True(t, tc.nor.NotAfter.After(time.Now().Add(24*time.Hour-time.Minute)))
  83. } else {
  84. assert.Equals(t, tc.nor.NotAfter, tc.naf)
  85. }
  86. }
  87. }
  88. })
  89. }
  90. }
  91. func TestFinalizeRequestValidate(t *testing.T) {
  92. _csr, err := pemutil.Read("../../authority/testdata/certs/foo.csr")
  93. assert.FatalError(t, err)
  94. csr, ok := _csr.(*x509.CertificateRequest)
  95. assert.Fatal(t, ok)
  96. type test struct {
  97. fr *FinalizeRequest
  98. err *acme.Error
  99. }
  100. var tests = map[string]func(t *testing.T) test{
  101. "fail/parse-csr-error": func(t *testing.T) test {
  102. return test{
  103. fr: &FinalizeRequest{},
  104. err: acme.NewError(acme.ErrorMalformedType, "unable to parse csr: asn1: syntax error: sequence truncated"),
  105. }
  106. },
  107. "fail/invalid-csr-signature": func(t *testing.T) test {
  108. b, err := pemutil.Read("../../authority/testdata/certs/badsig.csr")
  109. assert.FatalError(t, err)
  110. c, ok := b.(*x509.CertificateRequest)
  111. assert.Fatal(t, ok)
  112. return test{
  113. fr: &FinalizeRequest{
  114. CSR: base64.RawURLEncoding.EncodeToString(c.Raw),
  115. },
  116. err: acme.NewError(acme.ErrorMalformedType, "csr failed signature check: x509: ECDSA verification failure"),
  117. }
  118. },
  119. "ok": func(t *testing.T) test {
  120. return test{
  121. fr: &FinalizeRequest{
  122. CSR: base64.RawURLEncoding.EncodeToString(csr.Raw),
  123. },
  124. }
  125. },
  126. }
  127. for name, run := range tests {
  128. tc := run(t)
  129. t.Run(name, func(t *testing.T) {
  130. if err := tc.fr.Validate(); err != nil {
  131. if assert.NotNil(t, err) {
  132. ae, ok := err.(*acme.Error)
  133. assert.True(t, ok)
  134. assert.HasPrefix(t, ae.Error(), tc.err.Error())
  135. assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
  136. assert.Equals(t, ae.Type, tc.err.Type)
  137. }
  138. } else {
  139. if assert.Nil(t, tc.err) {
  140. assert.Equals(t, tc.fr.csr.Raw, csr.Raw)
  141. }
  142. }
  143. })
  144. }
  145. }
  146. func TestHandler_GetOrder(t *testing.T) {
  147. prov := newProv()
  148. escProvName := url.PathEscape(prov.GetName())
  149. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  150. now := clock.Now()
  151. nbf := now
  152. naf := now.Add(24 * time.Hour)
  153. expiry := now.Add(-time.Hour)
  154. o := acme.Order{
  155. ID: "orderID",
  156. NotBefore: nbf,
  157. NotAfter: naf,
  158. Identifiers: []acme.Identifier{
  159. {
  160. Type: "dns",
  161. Value: "example.com",
  162. },
  163. {
  164. Type: "dns",
  165. Value: "*.smallstep.com",
  166. },
  167. },
  168. ExpiresAt: expiry,
  169. Status: acme.StatusInvalid,
  170. Error: acme.NewError(acme.ErrorMalformedType, "order has expired"),
  171. AuthorizationURLs: []string{
  172. fmt.Sprintf("%s/acme/%s/authz/foo", baseURL.String(), escProvName),
  173. fmt.Sprintf("%s/acme/%s/authz/bar", baseURL.String(), escProvName),
  174. fmt.Sprintf("%s/acme/%s/authz/baz", baseURL.String(), escProvName),
  175. },
  176. FinalizeURL: fmt.Sprintf("%s/acme/%s/order/orderID/finalize", baseURL.String(), escProvName),
  177. }
  178. // Request with chi context
  179. chiCtx := chi.NewRouteContext()
  180. chiCtx.URLParams.Add("ordID", o.ID)
  181. url := fmt.Sprintf("%s/acme/%s/order/%s",
  182. baseURL.String(), escProvName, o.ID)
  183. type test struct {
  184. db acme.DB
  185. ctx context.Context
  186. statusCode int
  187. err *acme.Error
  188. }
  189. var tests = map[string]func(t *testing.T) test{
  190. "fail/no-account": func(t *testing.T) test {
  191. return test{
  192. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  193. statusCode: 400,
  194. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  195. }
  196. },
  197. "fail/nil-account": func(t *testing.T) test {
  198. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  199. ctx = context.WithValue(ctx, accContextKey, nil)
  200. return test{
  201. ctx: ctx,
  202. statusCode: 400,
  203. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  204. }
  205. },
  206. "fail/no-provisioner": func(t *testing.T) test {
  207. acc := &acme.Account{ID: "accountID"}
  208. ctx := context.WithValue(context.Background(), accContextKey, acc)
  209. return test{
  210. ctx: ctx,
  211. statusCode: 500,
  212. err: acme.NewErrorISE("provisioner does not exist"),
  213. }
  214. },
  215. "fail/nil-provisioner": func(t *testing.T) test {
  216. acc := &acme.Account{ID: "accountID"}
  217. ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
  218. ctx = context.WithValue(ctx, accContextKey, acc)
  219. return test{
  220. ctx: ctx,
  221. statusCode: 500,
  222. err: acme.NewErrorISE("provisioner does not exist"),
  223. }
  224. },
  225. "fail/db.GetOrder-error": func(t *testing.T) test {
  226. acc := &acme.Account{ID: "accountID"}
  227. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  228. ctx = context.WithValue(ctx, accContextKey, acc)
  229. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  230. return test{
  231. db: &acme.MockDB{
  232. MockError: acme.NewErrorISE("force"),
  233. },
  234. ctx: ctx,
  235. statusCode: 500,
  236. err: acme.NewErrorISE("force"),
  237. }
  238. },
  239. "fail/account-id-mismatch": func(t *testing.T) test {
  240. acc := &acme.Account{ID: "accountID"}
  241. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  242. ctx = context.WithValue(ctx, accContextKey, acc)
  243. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  244. return test{
  245. db: &acme.MockDB{
  246. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  247. return &acme.Order{AccountID: "foo"}, nil
  248. },
  249. },
  250. ctx: ctx,
  251. statusCode: 401,
  252. err: acme.NewError(acme.ErrorUnauthorizedType, "account id mismatch"),
  253. }
  254. },
  255. "fail/provisioner-id-mismatch": func(t *testing.T) test {
  256. acc := &acme.Account{ID: "accountID"}
  257. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  258. ctx = context.WithValue(ctx, accContextKey, acc)
  259. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  260. return test{
  261. db: &acme.MockDB{
  262. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  263. return &acme.Order{AccountID: "accountID", ProvisionerID: "bar"}, nil
  264. },
  265. },
  266. ctx: ctx,
  267. statusCode: 401,
  268. err: acme.NewError(acme.ErrorUnauthorizedType, "provisioner id mismatch"),
  269. }
  270. },
  271. "fail/order-update-error": func(t *testing.T) test {
  272. acc := &acme.Account{ID: "accountID"}
  273. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  274. ctx = context.WithValue(ctx, accContextKey, acc)
  275. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  276. return test{
  277. db: &acme.MockDB{
  278. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  279. return &acme.Order{
  280. AccountID: "accountID",
  281. ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
  282. ExpiresAt: clock.Now().Add(-time.Hour),
  283. Status: acme.StatusReady,
  284. }, nil
  285. },
  286. MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
  287. return acme.NewErrorISE("force")
  288. },
  289. },
  290. ctx: ctx,
  291. statusCode: 500,
  292. err: acme.NewErrorISE("force"),
  293. }
  294. },
  295. "ok": func(t *testing.T) test {
  296. acc := &acme.Account{ID: "accountID"}
  297. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  298. ctx = context.WithValue(ctx, accContextKey, acc)
  299. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  300. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  301. return test{
  302. db: &acme.MockDB{
  303. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  304. return &acme.Order{
  305. ID: "orderID",
  306. AccountID: "accountID",
  307. ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
  308. ExpiresAt: expiry,
  309. Status: acme.StatusReady,
  310. AuthorizationIDs: []string{"foo", "bar", "baz"},
  311. NotBefore: nbf,
  312. NotAfter: naf,
  313. Identifiers: []acme.Identifier{
  314. {
  315. Type: "dns",
  316. Value: "example.com",
  317. },
  318. {
  319. Type: "dns",
  320. Value: "*.smallstep.com",
  321. },
  322. },
  323. }, nil
  324. },
  325. MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
  326. return nil
  327. },
  328. },
  329. ctx: ctx,
  330. statusCode: 200,
  331. }
  332. },
  333. }
  334. for name, run := range tests {
  335. tc := run(t)
  336. t.Run(name, func(t *testing.T) {
  337. h := &Handler{linker: NewLinker("dns", "acme"), db: tc.db}
  338. req := httptest.NewRequest("GET", url, nil)
  339. req = req.WithContext(tc.ctx)
  340. w := httptest.NewRecorder()
  341. h.GetOrder(w, req)
  342. res := w.Result()
  343. assert.Equals(t, res.StatusCode, tc.statusCode)
  344. body, err := ioutil.ReadAll(res.Body)
  345. res.Body.Close()
  346. assert.FatalError(t, err)
  347. if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
  348. var ae acme.Error
  349. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  350. assert.Equals(t, ae.Type, tc.err.Type)
  351. assert.Equals(t, ae.Detail, tc.err.Detail)
  352. assert.Equals(t, ae.Identifier, tc.err.Identifier)
  353. assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
  354. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  355. } else {
  356. expB, err := json.Marshal(o)
  357. assert.FatalError(t, err)
  358. assert.Equals(t, bytes.TrimSpace(body), expB)
  359. assert.Equals(t, res.Header["Location"], []string{url})
  360. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  361. }
  362. })
  363. }
  364. }
  365. func TestHandler_newAuthorization(t *testing.T) {
  366. type test struct {
  367. az *acme.Authorization
  368. db acme.DB
  369. err *acme.Error
  370. }
  371. var tests = map[string]func(t *testing.T) test{
  372. "fail/error-db.CreateChallenge": func(t *testing.T) test {
  373. az := &acme.Authorization{
  374. AccountID: "accID",
  375. Identifier: acme.Identifier{
  376. Type: "dns",
  377. Value: "zap.internal",
  378. },
  379. }
  380. return test{
  381. db: &acme.MockDB{
  382. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  383. assert.Equals(t, ch.AccountID, az.AccountID)
  384. assert.Equals(t, ch.Type, "dns-01")
  385. assert.Equals(t, ch.Token, az.Token)
  386. assert.Equals(t, ch.Status, acme.StatusPending)
  387. assert.Equals(t, ch.Value, az.Identifier.Value)
  388. return errors.New("force")
  389. },
  390. },
  391. az: az,
  392. err: acme.NewErrorISE("error creating challenge: force"),
  393. }
  394. },
  395. "fail/error-db.CreateAuthorization": func(t *testing.T) test {
  396. az := &acme.Authorization{
  397. AccountID: "accID",
  398. Identifier: acme.Identifier{
  399. Type: "dns",
  400. Value: "zap.internal",
  401. },
  402. Status: acme.StatusPending,
  403. ExpiresAt: clock.Now(),
  404. }
  405. count := 0
  406. var ch1, ch2, ch3 **acme.Challenge
  407. return test{
  408. db: &acme.MockDB{
  409. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  410. switch count {
  411. case 0:
  412. ch.ID = "dns"
  413. assert.Equals(t, ch.Type, "dns-01")
  414. ch1 = &ch
  415. case 1:
  416. ch.ID = "http"
  417. assert.Equals(t, ch.Type, "http-01")
  418. ch2 = &ch
  419. case 2:
  420. ch.ID = "tls"
  421. assert.Equals(t, ch.Type, "tls-alpn-01")
  422. ch3 = &ch
  423. default:
  424. assert.FatalError(t, errors.New("test logic error"))
  425. return errors.New("force")
  426. }
  427. count++
  428. assert.Equals(t, ch.AccountID, az.AccountID)
  429. assert.Equals(t, ch.Token, az.Token)
  430. assert.Equals(t, ch.Status, acme.StatusPending)
  431. assert.Equals(t, ch.Value, az.Identifier.Value)
  432. return nil
  433. },
  434. MockCreateAuthorization: func(ctx context.Context, _az *acme.Authorization) error {
  435. assert.Equals(t, _az.AccountID, az.AccountID)
  436. assert.Equals(t, _az.Token, az.Token)
  437. assert.Equals(t, _az.Status, acme.StatusPending)
  438. assert.Equals(t, _az.Identifier, az.Identifier)
  439. assert.Equals(t, _az.ExpiresAt, az.ExpiresAt)
  440. assert.Equals(t, _az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  441. assert.Equals(t, _az.Wildcard, false)
  442. return errors.New("force")
  443. },
  444. },
  445. az: az,
  446. err: acme.NewErrorISE("error creating authorization: force"),
  447. }
  448. },
  449. "ok/no-wildcard": func(t *testing.T) test {
  450. az := &acme.Authorization{
  451. AccountID: "accID",
  452. Identifier: acme.Identifier{
  453. Type: "dns",
  454. Value: "zap.internal",
  455. },
  456. Status: acme.StatusPending,
  457. ExpiresAt: clock.Now(),
  458. }
  459. count := 0
  460. var ch1, ch2, ch3 **acme.Challenge
  461. return test{
  462. db: &acme.MockDB{
  463. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  464. switch count {
  465. case 0:
  466. ch.ID = "dns"
  467. assert.Equals(t, ch.Type, "dns-01")
  468. ch1 = &ch
  469. case 1:
  470. ch.ID = "http"
  471. assert.Equals(t, ch.Type, "http-01")
  472. ch2 = &ch
  473. case 2:
  474. ch.ID = "tls"
  475. assert.Equals(t, ch.Type, "tls-alpn-01")
  476. ch3 = &ch
  477. default:
  478. assert.FatalError(t, errors.New("test logic error"))
  479. return errors.New("force")
  480. }
  481. count++
  482. assert.Equals(t, ch.AccountID, az.AccountID)
  483. assert.Equals(t, ch.Token, az.Token)
  484. assert.Equals(t, ch.Status, acme.StatusPending)
  485. assert.Equals(t, ch.Value, az.Identifier.Value)
  486. return nil
  487. },
  488. MockCreateAuthorization: func(ctx context.Context, _az *acme.Authorization) error {
  489. assert.Equals(t, _az.AccountID, az.AccountID)
  490. assert.Equals(t, _az.Token, az.Token)
  491. assert.Equals(t, _az.Status, acme.StatusPending)
  492. assert.Equals(t, _az.Identifier, az.Identifier)
  493. assert.Equals(t, _az.ExpiresAt, az.ExpiresAt)
  494. assert.Equals(t, _az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  495. assert.Equals(t, _az.Wildcard, false)
  496. return nil
  497. },
  498. },
  499. az: az,
  500. }
  501. },
  502. "ok/wildcard": func(t *testing.T) test {
  503. az := &acme.Authorization{
  504. AccountID: "accID",
  505. Identifier: acme.Identifier{
  506. Type: "dns",
  507. Value: "*.zap.internal",
  508. },
  509. Status: acme.StatusPending,
  510. ExpiresAt: clock.Now(),
  511. }
  512. var ch1 **acme.Challenge
  513. return test{
  514. db: &acme.MockDB{
  515. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  516. ch.ID = "dns"
  517. assert.Equals(t, ch.Type, "dns-01")
  518. assert.Equals(t, ch.AccountID, az.AccountID)
  519. assert.Equals(t, ch.Token, az.Token)
  520. assert.Equals(t, ch.Status, acme.StatusPending)
  521. assert.Equals(t, ch.Value, "zap.internal")
  522. ch1 = &ch
  523. return nil
  524. },
  525. MockCreateAuthorization: func(ctx context.Context, _az *acme.Authorization) error {
  526. assert.Equals(t, _az.AccountID, az.AccountID)
  527. assert.Equals(t, _az.Token, az.Token)
  528. assert.Equals(t, _az.Status, acme.StatusPending)
  529. assert.Equals(t, _az.Identifier, acme.Identifier{
  530. Type: "dns",
  531. Value: "zap.internal",
  532. })
  533. assert.Equals(t, _az.ExpiresAt, az.ExpiresAt)
  534. assert.Equals(t, _az.Challenges, []*acme.Challenge{*ch1})
  535. assert.Equals(t, _az.Wildcard, true)
  536. return nil
  537. },
  538. },
  539. az: az,
  540. }
  541. },
  542. }
  543. for name, run := range tests {
  544. t.Run(name, func(t *testing.T) {
  545. tc := run(t)
  546. h := &Handler{db: tc.db}
  547. if err := h.newAuthorization(context.Background(), tc.az); err != nil {
  548. if assert.NotNil(t, tc.err) {
  549. switch k := err.(type) {
  550. case *acme.Error:
  551. assert.Equals(t, k.Type, tc.err.Type)
  552. assert.Equals(t, k.Detail, tc.err.Detail)
  553. assert.Equals(t, k.Status, tc.err.Status)
  554. assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
  555. assert.Equals(t, k.Detail, tc.err.Detail)
  556. default:
  557. assert.FatalError(t, errors.New("unexpected error type"))
  558. }
  559. }
  560. } else {
  561. assert.Nil(t, tc.err)
  562. }
  563. })
  564. }
  565. }
  566. func TestHandler_NewOrder(t *testing.T) {
  567. // Request with chi context
  568. prov := newProv()
  569. escProvName := url.PathEscape(prov.GetName())
  570. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  571. url := fmt.Sprintf("%s/acme/%s/order/ordID",
  572. baseURL.String(), escProvName)
  573. type test struct {
  574. db acme.DB
  575. ctx context.Context
  576. nor *NewOrderRequest
  577. statusCode int
  578. vr func(t *testing.T, o *acme.Order)
  579. err *acme.Error
  580. }
  581. var tests = map[string]func(t *testing.T) test{
  582. "fail/no-account": func(t *testing.T) test {
  583. return test{
  584. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  585. statusCode: 400,
  586. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  587. }
  588. },
  589. "fail/nil-account": func(t *testing.T) test {
  590. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  591. ctx = context.WithValue(ctx, accContextKey, nil)
  592. return test{
  593. ctx: ctx,
  594. statusCode: 400,
  595. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  596. }
  597. },
  598. "fail/no-provisioner": func(t *testing.T) test {
  599. acc := &acme.Account{ID: "accountID"}
  600. ctx := context.WithValue(context.Background(), accContextKey, acc)
  601. return test{
  602. ctx: ctx,
  603. statusCode: 500,
  604. err: acme.NewErrorISE("provisioner does not exist"),
  605. }
  606. },
  607. "fail/nil-provisioner": func(t *testing.T) test {
  608. acc := &acme.Account{ID: "accountID"}
  609. ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
  610. ctx = context.WithValue(ctx, accContextKey, acc)
  611. return test{
  612. ctx: ctx,
  613. statusCode: 500,
  614. err: acme.NewErrorISE("provisioner does not exist"),
  615. }
  616. },
  617. "fail/no-payload": func(t *testing.T) test {
  618. acc := &acme.Account{ID: "accountID"}
  619. ctx := context.WithValue(context.Background(), accContextKey, acc)
  620. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  621. return test{
  622. ctx: ctx,
  623. statusCode: 500,
  624. err: acme.NewErrorISE("payload does not exist"),
  625. }
  626. },
  627. "fail/nil-payload": func(t *testing.T) test {
  628. acc := &acme.Account{ID: "accountID"}
  629. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  630. ctx = context.WithValue(ctx, accContextKey, acc)
  631. ctx = context.WithValue(ctx, payloadContextKey, nil)
  632. return test{
  633. ctx: ctx,
  634. statusCode: 500,
  635. err: acme.NewErrorISE("paylod does not exist"),
  636. }
  637. },
  638. "fail/unmarshal-payload-error": func(t *testing.T) test {
  639. acc := &acme.Account{ID: "accID"}
  640. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  641. ctx = context.WithValue(ctx, accContextKey, acc)
  642. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
  643. return test{
  644. ctx: ctx,
  645. statusCode: 400,
  646. err: acme.NewError(acme.ErrorMalformedType, "failed to unmarshal new-order request payload: unexpected end of JSON input"),
  647. }
  648. },
  649. "fail/malformed-payload-error": func(t *testing.T) test {
  650. acc := &acme.Account{ID: "accID"}
  651. fr := &NewOrderRequest{}
  652. b, err := json.Marshal(fr)
  653. assert.FatalError(t, err)
  654. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  655. ctx = context.WithValue(ctx, accContextKey, acc)
  656. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  657. return test{
  658. ctx: ctx,
  659. statusCode: 400,
  660. err: acme.NewError(acme.ErrorMalformedType, "identifiers list cannot be empty"),
  661. }
  662. },
  663. "fail/error-h.newAuthorization": func(t *testing.T) test {
  664. acc := &acme.Account{ID: "accID"}
  665. fr := &NewOrderRequest{
  666. Identifiers: []acme.Identifier{
  667. {Type: "dns", Value: "zap.internal"},
  668. },
  669. }
  670. b, err := json.Marshal(fr)
  671. assert.FatalError(t, err)
  672. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  673. ctx = context.WithValue(ctx, accContextKey, acc)
  674. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  675. return test{
  676. ctx: ctx,
  677. statusCode: 500,
  678. db: &acme.MockDB{
  679. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  680. assert.Equals(t, ch.AccountID, "accID")
  681. assert.Equals(t, ch.Type, "dns-01")
  682. assert.NotEquals(t, ch.Token, "")
  683. assert.Equals(t, ch.Status, acme.StatusPending)
  684. assert.Equals(t, ch.Value, "zap.internal")
  685. return errors.New("force")
  686. },
  687. },
  688. err: acme.NewErrorISE("error creating challenge: force"),
  689. }
  690. },
  691. "fail/error-db.CreateOrder": func(t *testing.T) test {
  692. acc := &acme.Account{ID: "accID"}
  693. fr := &NewOrderRequest{
  694. Identifiers: []acme.Identifier{
  695. {Type: "dns", Value: "zap.internal"},
  696. },
  697. }
  698. b, err := json.Marshal(fr)
  699. assert.FatalError(t, err)
  700. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  701. ctx = context.WithValue(ctx, accContextKey, acc)
  702. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  703. var (
  704. ch1, ch2, ch3 **acme.Challenge
  705. az1ID *string
  706. count = 0
  707. )
  708. return test{
  709. ctx: ctx,
  710. statusCode: 500,
  711. db: &acme.MockDB{
  712. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  713. switch count {
  714. case 0:
  715. ch.ID = "dns"
  716. assert.Equals(t, ch.Type, "dns-01")
  717. ch1 = &ch
  718. case 1:
  719. ch.ID = "http"
  720. assert.Equals(t, ch.Type, "http-01")
  721. ch2 = &ch
  722. case 2:
  723. ch.ID = "tls"
  724. assert.Equals(t, ch.Type, "tls-alpn-01")
  725. ch3 = &ch
  726. default:
  727. assert.FatalError(t, errors.New("test logic error"))
  728. return errors.New("force")
  729. }
  730. count++
  731. assert.Equals(t, ch.AccountID, "accID")
  732. assert.NotEquals(t, ch.Token, "")
  733. assert.Equals(t, ch.Status, acme.StatusPending)
  734. assert.Equals(t, ch.Value, "zap.internal")
  735. return nil
  736. },
  737. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  738. az.ID = "az1ID"
  739. az1ID = &az.ID
  740. assert.Equals(t, az.AccountID, "accID")
  741. assert.NotEquals(t, az.Token, "")
  742. assert.Equals(t, az.Status, acme.StatusPending)
  743. assert.Equals(t, az.Identifier, fr.Identifiers[0])
  744. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  745. assert.Equals(t, az.Wildcard, false)
  746. return nil
  747. },
  748. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  749. assert.Equals(t, o.AccountID, "accID")
  750. assert.Equals(t, o.ProvisionerID, prov.GetID())
  751. assert.Equals(t, o.Status, acme.StatusPending)
  752. assert.Equals(t, o.Identifiers, fr.Identifiers)
  753. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  754. return errors.New("force")
  755. },
  756. },
  757. err: acme.NewErrorISE("error creating order: force"),
  758. }
  759. },
  760. "ok/multiple-authz": func(t *testing.T) test {
  761. acc := &acme.Account{ID: "accID"}
  762. nor := &NewOrderRequest{
  763. Identifiers: []acme.Identifier{
  764. {Type: "dns", Value: "zap.internal"},
  765. {Type: "dns", Value: "*.zar.internal"},
  766. },
  767. }
  768. b, err := json.Marshal(nor)
  769. assert.FatalError(t, err)
  770. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  771. ctx = context.WithValue(ctx, accContextKey, acc)
  772. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  773. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  774. var (
  775. ch1, ch2, ch3, ch4 **acme.Challenge
  776. az1ID, az2ID *string
  777. chCount, azCount = 0, 0
  778. )
  779. return test{
  780. ctx: ctx,
  781. statusCode: 201,
  782. nor: nor,
  783. db: &acme.MockDB{
  784. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  785. switch chCount {
  786. case 0:
  787. ch.ID = "dns"
  788. assert.Equals(t, ch.Type, "dns-01")
  789. assert.Equals(t, ch.Value, "zap.internal")
  790. ch1 = &ch
  791. case 1:
  792. ch.ID = "http"
  793. assert.Equals(t, ch.Type, "http-01")
  794. assert.Equals(t, ch.Value, "zap.internal")
  795. ch2 = &ch
  796. case 2:
  797. ch.ID = "tls"
  798. assert.Equals(t, ch.Type, "tls-alpn-01")
  799. assert.Equals(t, ch.Value, "zap.internal")
  800. ch3 = &ch
  801. case 3:
  802. ch.ID = "dns"
  803. assert.Equals(t, ch.Type, "dns-01")
  804. assert.Equals(t, ch.Value, "zar.internal")
  805. ch4 = &ch
  806. default:
  807. assert.FatalError(t, errors.New("test logic error"))
  808. return errors.New("force")
  809. }
  810. chCount++
  811. assert.Equals(t, ch.AccountID, "accID")
  812. assert.NotEquals(t, ch.Token, "")
  813. assert.Equals(t, ch.Status, acme.StatusPending)
  814. return nil
  815. },
  816. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  817. switch azCount {
  818. case 0:
  819. az.ID = "az1ID"
  820. az1ID = &az.ID
  821. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  822. assert.Equals(t, az.Wildcard, false)
  823. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  824. case 1:
  825. az.ID = "az2ID"
  826. az2ID = &az.ID
  827. assert.Equals(t, az.Identifier, acme.Identifier{
  828. Type: "dns",
  829. Value: "zar.internal",
  830. })
  831. assert.Equals(t, az.Wildcard, true)
  832. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch4})
  833. default:
  834. assert.FatalError(t, errors.New("test logic error"))
  835. return errors.New("force")
  836. }
  837. azCount++
  838. assert.Equals(t, az.AccountID, "accID")
  839. assert.NotEquals(t, az.Token, "")
  840. assert.Equals(t, az.Status, acme.StatusPending)
  841. return nil
  842. },
  843. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  844. o.ID = "ordID"
  845. assert.Equals(t, o.AccountID, "accID")
  846. assert.Equals(t, o.ProvisionerID, prov.GetID())
  847. assert.Equals(t, o.Status, acme.StatusPending)
  848. assert.Equals(t, o.Identifiers, nor.Identifiers)
  849. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID, *az2ID})
  850. return nil
  851. },
  852. },
  853. vr: func(t *testing.T, o *acme.Order) {
  854. now := clock.Now()
  855. testBufferDur := 5 * time.Second
  856. orderExpiry := now.Add(defaultOrderExpiry)
  857. expNbf := now.Add(-defaultOrderBackdate)
  858. expNaf := now.Add(prov.DefaultTLSCertDuration())
  859. assert.Equals(t, o.ID, "ordID")
  860. assert.Equals(t, o.Status, acme.StatusPending)
  861. assert.Equals(t, o.Identifiers, nor.Identifiers)
  862. assert.Equals(t, o.AuthorizationURLs, []string{
  863. fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName),
  864. fmt.Sprintf("%s/acme/%s/authz/az2ID", baseURL.String(), escProvName),
  865. })
  866. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  867. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  868. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  869. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  870. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  871. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  872. },
  873. }
  874. },
  875. "ok/default-naf-nbf": func(t *testing.T) test {
  876. acc := &acme.Account{ID: "accID"}
  877. nor := &NewOrderRequest{
  878. Identifiers: []acme.Identifier{
  879. {Type: "dns", Value: "zap.internal"},
  880. },
  881. }
  882. b, err := json.Marshal(nor)
  883. assert.FatalError(t, err)
  884. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  885. ctx = context.WithValue(ctx, accContextKey, acc)
  886. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  887. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  888. var (
  889. ch1, ch2, ch3 **acme.Challenge
  890. az1ID *string
  891. count = 0
  892. )
  893. return test{
  894. ctx: ctx,
  895. statusCode: 201,
  896. nor: nor,
  897. db: &acme.MockDB{
  898. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  899. switch count {
  900. case 0:
  901. ch.ID = "dns"
  902. assert.Equals(t, ch.Type, "dns-01")
  903. ch1 = &ch
  904. case 1:
  905. ch.ID = "http"
  906. assert.Equals(t, ch.Type, "http-01")
  907. ch2 = &ch
  908. case 2:
  909. ch.ID = "tls"
  910. assert.Equals(t, ch.Type, "tls-alpn-01")
  911. ch3 = &ch
  912. default:
  913. assert.FatalError(t, errors.New("test logic error"))
  914. return errors.New("force")
  915. }
  916. count++
  917. assert.Equals(t, ch.AccountID, "accID")
  918. assert.NotEquals(t, ch.Token, "")
  919. assert.Equals(t, ch.Status, acme.StatusPending)
  920. assert.Equals(t, ch.Value, "zap.internal")
  921. return nil
  922. },
  923. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  924. az.ID = "az1ID"
  925. az1ID = &az.ID
  926. assert.Equals(t, az.AccountID, "accID")
  927. assert.NotEquals(t, az.Token, "")
  928. assert.Equals(t, az.Status, acme.StatusPending)
  929. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  930. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  931. assert.Equals(t, az.Wildcard, false)
  932. return nil
  933. },
  934. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  935. o.ID = "ordID"
  936. assert.Equals(t, o.AccountID, "accID")
  937. assert.Equals(t, o.ProvisionerID, prov.GetID())
  938. assert.Equals(t, o.Status, acme.StatusPending)
  939. assert.Equals(t, o.Identifiers, nor.Identifiers)
  940. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  941. return nil
  942. },
  943. },
  944. vr: func(t *testing.T, o *acme.Order) {
  945. now := clock.Now()
  946. testBufferDur := 5 * time.Second
  947. orderExpiry := now.Add(defaultOrderExpiry)
  948. expNbf := now.Add(-defaultOrderBackdate)
  949. expNaf := now.Add(prov.DefaultTLSCertDuration())
  950. assert.Equals(t, o.ID, "ordID")
  951. assert.Equals(t, o.Status, acme.StatusPending)
  952. assert.Equals(t, o.Identifiers, nor.Identifiers)
  953. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  954. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  955. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  956. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  957. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  958. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  959. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  960. },
  961. }
  962. },
  963. "ok/nbf-no-naf": func(t *testing.T) test {
  964. now := clock.Now()
  965. expNbf := now.Add(10 * time.Minute)
  966. acc := &acme.Account{ID: "accID"}
  967. nor := &NewOrderRequest{
  968. Identifiers: []acme.Identifier{
  969. {Type: "dns", Value: "zap.internal"},
  970. },
  971. NotBefore: expNbf,
  972. }
  973. b, err := json.Marshal(nor)
  974. assert.FatalError(t, err)
  975. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  976. ctx = context.WithValue(ctx, accContextKey, acc)
  977. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  978. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  979. var (
  980. ch1, ch2, ch3 **acme.Challenge
  981. az1ID *string
  982. count = 0
  983. )
  984. return test{
  985. ctx: ctx,
  986. statusCode: 201,
  987. nor: nor,
  988. db: &acme.MockDB{
  989. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  990. switch count {
  991. case 0:
  992. ch.ID = "dns"
  993. assert.Equals(t, ch.Type, "dns-01")
  994. ch1 = &ch
  995. case 1:
  996. ch.ID = "http"
  997. assert.Equals(t, ch.Type, "http-01")
  998. ch2 = &ch
  999. case 2:
  1000. ch.ID = "tls"
  1001. assert.Equals(t, ch.Type, "tls-alpn-01")
  1002. ch3 = &ch
  1003. default:
  1004. assert.FatalError(t, errors.New("test logic error"))
  1005. return errors.New("force")
  1006. }
  1007. count++
  1008. assert.Equals(t, ch.AccountID, "accID")
  1009. assert.NotEquals(t, ch.Token, "")
  1010. assert.Equals(t, ch.Status, acme.StatusPending)
  1011. assert.Equals(t, ch.Value, "zap.internal")
  1012. return nil
  1013. },
  1014. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  1015. az.ID = "az1ID"
  1016. az1ID = &az.ID
  1017. assert.Equals(t, az.AccountID, "accID")
  1018. assert.NotEquals(t, az.Token, "")
  1019. assert.Equals(t, az.Status, acme.StatusPending)
  1020. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  1021. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  1022. assert.Equals(t, az.Wildcard, false)
  1023. return nil
  1024. },
  1025. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  1026. o.ID = "ordID"
  1027. assert.Equals(t, o.AccountID, "accID")
  1028. assert.Equals(t, o.ProvisionerID, prov.GetID())
  1029. assert.Equals(t, o.Status, acme.StatusPending)
  1030. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1031. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  1032. return nil
  1033. },
  1034. },
  1035. vr: func(t *testing.T, o *acme.Order) {
  1036. now := clock.Now()
  1037. testBufferDur := 5 * time.Second
  1038. orderExpiry := now.Add(defaultOrderExpiry)
  1039. expNaf := expNbf.Add(prov.DefaultTLSCertDuration())
  1040. assert.Equals(t, o.ID, "ordID")
  1041. assert.Equals(t, o.Status, acme.StatusPending)
  1042. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1043. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  1044. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  1045. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  1046. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  1047. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  1048. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  1049. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  1050. },
  1051. }
  1052. },
  1053. "ok/naf-no-nbf": func(t *testing.T) test {
  1054. now := clock.Now()
  1055. expNaf := now.Add(15 * time.Minute)
  1056. acc := &acme.Account{ID: "accID"}
  1057. nor := &NewOrderRequest{
  1058. Identifiers: []acme.Identifier{
  1059. {Type: "dns", Value: "zap.internal"},
  1060. },
  1061. NotAfter: expNaf,
  1062. }
  1063. b, err := json.Marshal(nor)
  1064. assert.FatalError(t, err)
  1065. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1066. ctx = context.WithValue(ctx, accContextKey, acc)
  1067. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  1068. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  1069. var (
  1070. ch1, ch2, ch3 **acme.Challenge
  1071. az1ID *string
  1072. count = 0
  1073. )
  1074. return test{
  1075. ctx: ctx,
  1076. statusCode: 201,
  1077. nor: nor,
  1078. db: &acme.MockDB{
  1079. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  1080. switch count {
  1081. case 0:
  1082. ch.ID = "dns"
  1083. assert.Equals(t, ch.Type, "dns-01")
  1084. ch1 = &ch
  1085. case 1:
  1086. ch.ID = "http"
  1087. assert.Equals(t, ch.Type, "http-01")
  1088. ch2 = &ch
  1089. case 2:
  1090. ch.ID = "tls"
  1091. assert.Equals(t, ch.Type, "tls-alpn-01")
  1092. ch3 = &ch
  1093. default:
  1094. assert.FatalError(t, errors.New("test logic error"))
  1095. return errors.New("force")
  1096. }
  1097. count++
  1098. assert.Equals(t, ch.AccountID, "accID")
  1099. assert.NotEquals(t, ch.Token, "")
  1100. assert.Equals(t, ch.Status, acme.StatusPending)
  1101. assert.Equals(t, ch.Value, "zap.internal")
  1102. return nil
  1103. },
  1104. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  1105. az.ID = "az1ID"
  1106. az1ID = &az.ID
  1107. assert.Equals(t, az.AccountID, "accID")
  1108. assert.NotEquals(t, az.Token, "")
  1109. assert.Equals(t, az.Status, acme.StatusPending)
  1110. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  1111. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  1112. assert.Equals(t, az.Wildcard, false)
  1113. return nil
  1114. },
  1115. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  1116. o.ID = "ordID"
  1117. assert.Equals(t, o.AccountID, "accID")
  1118. assert.Equals(t, o.ProvisionerID, prov.GetID())
  1119. assert.Equals(t, o.Status, acme.StatusPending)
  1120. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1121. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  1122. return nil
  1123. },
  1124. },
  1125. vr: func(t *testing.T, o *acme.Order) {
  1126. testBufferDur := 5 * time.Second
  1127. orderExpiry := now.Add(defaultOrderExpiry)
  1128. expNbf := now.Add(-defaultOrderBackdate)
  1129. assert.Equals(t, o.ID, "ordID")
  1130. assert.Equals(t, o.Status, acme.StatusPending)
  1131. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1132. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  1133. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  1134. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  1135. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  1136. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  1137. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  1138. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  1139. },
  1140. }
  1141. },
  1142. "ok/naf-nbf": func(t *testing.T) test {
  1143. now := clock.Now()
  1144. expNbf := now.Add(5 * time.Minute)
  1145. expNaf := now.Add(15 * time.Minute)
  1146. acc := &acme.Account{ID: "accID"}
  1147. nor := &NewOrderRequest{
  1148. Identifiers: []acme.Identifier{
  1149. {Type: "dns", Value: "zap.internal"},
  1150. },
  1151. NotBefore: expNbf,
  1152. NotAfter: expNaf,
  1153. }
  1154. b, err := json.Marshal(nor)
  1155. assert.FatalError(t, err)
  1156. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1157. ctx = context.WithValue(ctx, accContextKey, acc)
  1158. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  1159. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  1160. var (
  1161. ch1, ch2, ch3 **acme.Challenge
  1162. az1ID *string
  1163. count = 0
  1164. )
  1165. return test{
  1166. ctx: ctx,
  1167. statusCode: 201,
  1168. nor: nor,
  1169. db: &acme.MockDB{
  1170. MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
  1171. switch count {
  1172. case 0:
  1173. ch.ID = "dns"
  1174. assert.Equals(t, ch.Type, "dns-01")
  1175. ch1 = &ch
  1176. case 1:
  1177. ch.ID = "http"
  1178. assert.Equals(t, ch.Type, "http-01")
  1179. ch2 = &ch
  1180. case 2:
  1181. ch.ID = "tls"
  1182. assert.Equals(t, ch.Type, "tls-alpn-01")
  1183. ch3 = &ch
  1184. default:
  1185. assert.FatalError(t, errors.New("test logic error"))
  1186. return errors.New("force")
  1187. }
  1188. count++
  1189. assert.Equals(t, ch.AccountID, "accID")
  1190. assert.NotEquals(t, ch.Token, "")
  1191. assert.Equals(t, ch.Status, acme.StatusPending)
  1192. assert.Equals(t, ch.Value, "zap.internal")
  1193. return nil
  1194. },
  1195. MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
  1196. az.ID = "az1ID"
  1197. az1ID = &az.ID
  1198. assert.Equals(t, az.AccountID, "accID")
  1199. assert.NotEquals(t, az.Token, "")
  1200. assert.Equals(t, az.Status, acme.StatusPending)
  1201. assert.Equals(t, az.Identifier, nor.Identifiers[0])
  1202. assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
  1203. assert.Equals(t, az.Wildcard, false)
  1204. return nil
  1205. },
  1206. MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
  1207. o.ID = "ordID"
  1208. assert.Equals(t, o.AccountID, "accID")
  1209. assert.Equals(t, o.ProvisionerID, prov.GetID())
  1210. assert.Equals(t, o.Status, acme.StatusPending)
  1211. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1212. assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
  1213. return nil
  1214. },
  1215. },
  1216. vr: func(t *testing.T, o *acme.Order) {
  1217. testBufferDur := 5 * time.Second
  1218. orderExpiry := now.Add(defaultOrderExpiry)
  1219. assert.Equals(t, o.ID, "ordID")
  1220. assert.Equals(t, o.Status, acme.StatusPending)
  1221. assert.Equals(t, o.Identifiers, nor.Identifiers)
  1222. assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
  1223. assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
  1224. assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
  1225. assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
  1226. assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
  1227. assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
  1228. assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
  1229. },
  1230. }
  1231. },
  1232. }
  1233. for name, run := range tests {
  1234. tc := run(t)
  1235. t.Run(name, func(t *testing.T) {
  1236. h := &Handler{linker: NewLinker("dns", "acme"), db: tc.db}
  1237. req := httptest.NewRequest("GET", url, nil)
  1238. req = req.WithContext(tc.ctx)
  1239. w := httptest.NewRecorder()
  1240. h.NewOrder(w, req)
  1241. res := w.Result()
  1242. assert.Equals(t, res.StatusCode, tc.statusCode)
  1243. body, err := ioutil.ReadAll(res.Body)
  1244. res.Body.Close()
  1245. assert.FatalError(t, err)
  1246. if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
  1247. var ae acme.Error
  1248. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  1249. assert.Equals(t, ae.Type, tc.err.Type)
  1250. assert.Equals(t, ae.Detail, tc.err.Detail)
  1251. assert.Equals(t, ae.Identifier, tc.err.Identifier)
  1252. assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
  1253. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  1254. } else {
  1255. ro := new(acme.Order)
  1256. assert.FatalError(t, json.Unmarshal(body, ro))
  1257. if tc.vr != nil {
  1258. tc.vr(t, ro)
  1259. }
  1260. assert.Equals(t, res.Header["Location"], []string{url})
  1261. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  1262. }
  1263. })
  1264. }
  1265. }
  1266. func TestHandler_FinalizeOrder(t *testing.T) {
  1267. prov := newProv()
  1268. escProvName := url.PathEscape(prov.GetName())
  1269. baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
  1270. now := clock.Now()
  1271. nbf := now
  1272. naf := now.Add(24 * time.Hour)
  1273. o := acme.Order{
  1274. ID: "orderID",
  1275. NotBefore: nbf,
  1276. NotAfter: naf,
  1277. Identifiers: []acme.Identifier{
  1278. {
  1279. Type: "dns",
  1280. Value: "example.com",
  1281. },
  1282. {
  1283. Type: "dns",
  1284. Value: "*.smallstep.com",
  1285. },
  1286. },
  1287. ExpiresAt: naf,
  1288. Status: acme.StatusValid,
  1289. AuthorizationURLs: []string{
  1290. fmt.Sprintf("%s/acme/%s/authz/foo", baseURL.String(), escProvName),
  1291. fmt.Sprintf("%s/acme/%s/authz/bar", baseURL.String(), escProvName),
  1292. fmt.Sprintf("%s/acme/%s/authz/baz", baseURL.String(), escProvName),
  1293. },
  1294. FinalizeURL: fmt.Sprintf("%s/acme/%s/order/orderID/finalize", baseURL.String(), escProvName),
  1295. CertificateURL: fmt.Sprintf("%s/acme/%s/certificate/certID", baseURL.String(), escProvName),
  1296. }
  1297. // Request with chi context
  1298. chiCtx := chi.NewRouteContext()
  1299. chiCtx.URLParams.Add("ordID", o.ID)
  1300. url := fmt.Sprintf("%s/acme/%s/order/%s",
  1301. baseURL.String(), escProvName, o.ID)
  1302. _csr, err := pemutil.Read("../../authority/testdata/certs/foo.csr")
  1303. assert.FatalError(t, err)
  1304. csr, ok := _csr.(*x509.CertificateRequest)
  1305. assert.Fatal(t, ok)
  1306. nor := &FinalizeRequest{
  1307. CSR: base64.RawURLEncoding.EncodeToString(csr.Raw),
  1308. }
  1309. payloadBytes, err := json.Marshal(nor)
  1310. assert.FatalError(t, err)
  1311. type test struct {
  1312. db acme.DB
  1313. ctx context.Context
  1314. statusCode int
  1315. err *acme.Error
  1316. }
  1317. var tests = map[string]func(t *testing.T) test{
  1318. "fail/no-account": func(t *testing.T) test {
  1319. return test{
  1320. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  1321. statusCode: 400,
  1322. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  1323. }
  1324. },
  1325. "fail/nil-account": func(t *testing.T) test {
  1326. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1327. ctx = context.WithValue(ctx, accContextKey, nil)
  1328. return test{
  1329. ctx: ctx,
  1330. statusCode: 400,
  1331. err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
  1332. }
  1333. },
  1334. "fail/no-provisioner": func(t *testing.T) test {
  1335. acc := &acme.Account{ID: "accountID"}
  1336. ctx := context.WithValue(context.Background(), accContextKey, acc)
  1337. return test{
  1338. ctx: ctx,
  1339. statusCode: 500,
  1340. err: acme.NewErrorISE("provisioner does not exist"),
  1341. }
  1342. },
  1343. "fail/nil-provisioner": func(t *testing.T) test {
  1344. acc := &acme.Account{ID: "accountID"}
  1345. ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
  1346. ctx = context.WithValue(ctx, accContextKey, acc)
  1347. return test{
  1348. ctx: ctx,
  1349. statusCode: 500,
  1350. err: acme.NewErrorISE("provisioner does not exist"),
  1351. }
  1352. },
  1353. "fail/no-payload": func(t *testing.T) test {
  1354. acc := &acme.Account{ID: "accountID"}
  1355. ctx := context.WithValue(context.Background(), accContextKey, acc)
  1356. ctx = context.WithValue(ctx, provisionerContextKey, prov)
  1357. return test{
  1358. ctx: ctx,
  1359. statusCode: 500,
  1360. err: acme.NewErrorISE("payload does not exist"),
  1361. }
  1362. },
  1363. "fail/nil-payload": func(t *testing.T) test {
  1364. acc := &acme.Account{ID: "accountID"}
  1365. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1366. ctx = context.WithValue(ctx, accContextKey, acc)
  1367. ctx = context.WithValue(ctx, payloadContextKey, nil)
  1368. return test{
  1369. ctx: ctx,
  1370. statusCode: 500,
  1371. err: acme.NewErrorISE("paylod does not exist"),
  1372. }
  1373. },
  1374. "fail/unmarshal-payload-error": func(t *testing.T) test {
  1375. acc := &acme.Account{ID: "accID"}
  1376. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1377. ctx = context.WithValue(ctx, accContextKey, acc)
  1378. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
  1379. return test{
  1380. ctx: ctx,
  1381. statusCode: 400,
  1382. err: acme.NewError(acme.ErrorMalformedType, "failed to unmarshal finalize-order request payload: unexpected end of JSON input"),
  1383. }
  1384. },
  1385. "fail/malformed-payload-error": func(t *testing.T) test {
  1386. acc := &acme.Account{ID: "accID"}
  1387. fr := &FinalizeRequest{}
  1388. b, err := json.Marshal(fr)
  1389. assert.FatalError(t, err)
  1390. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1391. ctx = context.WithValue(ctx, accContextKey, acc)
  1392. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  1393. return test{
  1394. ctx: ctx,
  1395. statusCode: 400,
  1396. err: acme.NewError(acme.ErrorMalformedType, "unable to parse csr: asn1: syntax error: sequence truncated"),
  1397. }
  1398. },
  1399. "fail/db.GetOrder-error": func(t *testing.T) test {
  1400. acc := &acme.Account{ID: "accountID"}
  1401. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1402. ctx = context.WithValue(ctx, accContextKey, acc)
  1403. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1404. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1405. return test{
  1406. db: &acme.MockDB{
  1407. MockError: acme.NewErrorISE("force"),
  1408. },
  1409. ctx: ctx,
  1410. statusCode: 500,
  1411. err: acme.NewErrorISE("force"),
  1412. }
  1413. },
  1414. "fail/account-id-mismatch": func(t *testing.T) test {
  1415. acc := &acme.Account{ID: "accountID"}
  1416. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1417. ctx = context.WithValue(ctx, accContextKey, acc)
  1418. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1419. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1420. return test{
  1421. db: &acme.MockDB{
  1422. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  1423. return &acme.Order{AccountID: "foo"}, nil
  1424. },
  1425. },
  1426. ctx: ctx,
  1427. statusCode: 401,
  1428. err: acme.NewError(acme.ErrorUnauthorizedType, "account id mismatch"),
  1429. }
  1430. },
  1431. "fail/provisioner-id-mismatch": func(t *testing.T) test {
  1432. acc := &acme.Account{ID: "accountID"}
  1433. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1434. ctx = context.WithValue(ctx, accContextKey, acc)
  1435. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1436. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1437. return test{
  1438. db: &acme.MockDB{
  1439. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  1440. return &acme.Order{AccountID: "accountID", ProvisionerID: "bar"}, nil
  1441. },
  1442. },
  1443. ctx: ctx,
  1444. statusCode: 401,
  1445. err: acme.NewError(acme.ErrorUnauthorizedType, "provisioner id mismatch"),
  1446. }
  1447. },
  1448. "fail/order-finalize-error": func(t *testing.T) test {
  1449. acc := &acme.Account{ID: "accountID"}
  1450. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1451. ctx = context.WithValue(ctx, accContextKey, acc)
  1452. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1453. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1454. return test{
  1455. db: &acme.MockDB{
  1456. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  1457. return &acme.Order{
  1458. AccountID: "accountID",
  1459. ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
  1460. ExpiresAt: clock.Now().Add(-time.Hour),
  1461. Status: acme.StatusReady,
  1462. }, nil
  1463. },
  1464. MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
  1465. return acme.NewErrorISE("force")
  1466. },
  1467. },
  1468. ctx: ctx,
  1469. statusCode: 500,
  1470. err: acme.NewErrorISE("force"),
  1471. }
  1472. },
  1473. "ok": func(t *testing.T) test {
  1474. acc := &acme.Account{ID: "accountID"}
  1475. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  1476. ctx = context.WithValue(ctx, accContextKey, acc)
  1477. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
  1478. ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
  1479. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  1480. return test{
  1481. db: &acme.MockDB{
  1482. MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
  1483. return &acme.Order{
  1484. ID: "orderID",
  1485. AccountID: "accountID",
  1486. ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
  1487. ExpiresAt: naf,
  1488. Status: acme.StatusValid,
  1489. AuthorizationIDs: []string{"foo", "bar", "baz"},
  1490. NotBefore: nbf,
  1491. NotAfter: naf,
  1492. Identifiers: []acme.Identifier{
  1493. {
  1494. Type: "dns",
  1495. Value: "example.com",
  1496. },
  1497. {
  1498. Type: "dns",
  1499. Value: "*.smallstep.com",
  1500. },
  1501. },
  1502. CertificateID: "certID",
  1503. }, nil
  1504. },
  1505. },
  1506. ctx: ctx,
  1507. statusCode: 200,
  1508. }
  1509. },
  1510. }
  1511. for name, run := range tests {
  1512. tc := run(t)
  1513. t.Run(name, func(t *testing.T) {
  1514. h := &Handler{linker: NewLinker("dns", "acme"), db: tc.db}
  1515. req := httptest.NewRequest("GET", url, nil)
  1516. req = req.WithContext(tc.ctx)
  1517. w := httptest.NewRecorder()
  1518. h.FinalizeOrder(w, req)
  1519. res := w.Result()
  1520. assert.Equals(t, res.StatusCode, tc.statusCode)
  1521. body, err := ioutil.ReadAll(res.Body)
  1522. res.Body.Close()
  1523. assert.FatalError(t, err)
  1524. if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
  1525. var ae acme.Error
  1526. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  1527. assert.Equals(t, ae.Type, tc.err.Type)
  1528. assert.Equals(t, ae.Detail, tc.err.Detail)
  1529. assert.Equals(t, ae.Identifier, tc.err.Identifier)
  1530. assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
  1531. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  1532. } else {
  1533. expB, err := json.Marshal(o)
  1534. assert.FatalError(t, err)
  1535. ro := new(acme.Order)
  1536. assert.FatalError(t, json.Unmarshal(body, ro))
  1537. assert.Equals(t, bytes.TrimSpace(body), expB)
  1538. assert.Equals(t, res.Header["Location"], []string{url})
  1539. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  1540. }
  1541. })
  1542. }
  1543. }