account_test.go 21 KB

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