account_test.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http/httptest"
  9. "testing"
  10. "time"
  11. "github.com/go-chi/chi"
  12. "github.com/pkg/errors"
  13. "github.com/smallstep/assert"
  14. "github.com/smallstep/certificates/acme"
  15. "github.com/smallstep/certificates/authority/provisioner"
  16. "github.com/smallstep/cli/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() provisioner.Interface {
  28. // Initialize provisioners
  29. p := &provisioner.ACME{
  30. Type: "ACME",
  31. Name: "test@acme-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 TestNewAccountRequestValidate(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.MalformedErr(errors.Errorf("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.MalformedErr(errors.Errorf("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 TestUpdateAccountRequestValidate(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.MalformedErr(errors.Errorf("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.MalformedErr(errors.Errorf("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.MalformedErr(errors.Errorf("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. }
  141. for name, run := range tests {
  142. tc := run(t)
  143. t.Run(name, func(t *testing.T) {
  144. if err := tc.uar.Validate(); err != nil {
  145. if assert.NotNil(t, err) {
  146. ae, ok := err.(*acme.Error)
  147. assert.True(t, ok)
  148. assert.HasPrefix(t, ae.Error(), tc.err.Error())
  149. assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
  150. assert.Equals(t, ae.Type, tc.err.Type)
  151. }
  152. } else {
  153. assert.Nil(t, tc.err)
  154. }
  155. })
  156. }
  157. }
  158. func TestHandlerGetOrdersByAccount(t *testing.T) {
  159. oids := []string{
  160. "https://ca.smallstep.com/acme/order/foo",
  161. "https://ca.smallstep.com/acme/order/bar",
  162. }
  163. accID := "account-id"
  164. prov := newProv()
  165. // Request with chi context
  166. chiCtx := chi.NewRouteContext()
  167. chiCtx.URLParams.Add("accID", accID)
  168. url := fmt.Sprintf("http://ca.smallstep.com/acme/account/%s/orders", accID)
  169. type test struct {
  170. auth acme.Interface
  171. ctx context.Context
  172. statusCode int
  173. problem *acme.Error
  174. }
  175. var tests = map[string]func(t *testing.T) test{
  176. "fail/no-provisioner": func(t *testing.T) test {
  177. return test{
  178. auth: &mockAcmeAuthority{},
  179. ctx: context.Background(),
  180. statusCode: 500,
  181. problem: acme.ServerInternalErr(errors.Errorf("provisioner expected in request context")),
  182. }
  183. },
  184. "fail/nil-provisioner": func(t *testing.T) test {
  185. return test{
  186. auth: &mockAcmeAuthority{},
  187. ctx: context.WithValue(context.Background(), provisionerContextKey, nil),
  188. statusCode: 500,
  189. problem: acme.ServerInternalErr(errors.Errorf("provisioner expected in request context")),
  190. }
  191. },
  192. "fail/no-account": func(t *testing.T) test {
  193. return test{
  194. auth: &mockAcmeAuthority{},
  195. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  196. statusCode: 404,
  197. problem: acme.AccountDoesNotExistErr(nil),
  198. }
  199. },
  200. "fail/nil-account": func(t *testing.T) test {
  201. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  202. ctx = context.WithValue(ctx, accContextKey, nil)
  203. return test{
  204. auth: &mockAcmeAuthority{},
  205. ctx: ctx,
  206. statusCode: 404,
  207. problem: acme.AccountDoesNotExistErr(nil),
  208. }
  209. },
  210. "fail/account-id-mismatch": func(t *testing.T) test {
  211. acc := &acme.Account{ID: "foo"}
  212. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  213. ctx = context.WithValue(ctx, accContextKey, acc)
  214. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  215. return test{
  216. auth: &mockAcmeAuthority{},
  217. ctx: ctx,
  218. statusCode: 401,
  219. problem: acme.UnauthorizedErr(errors.New("account ID does not match url param")),
  220. }
  221. },
  222. "fail/getOrdersByAccount-error": func(t *testing.T) test {
  223. acc := &acme.Account{ID: accID}
  224. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  225. ctx = context.WithValue(ctx, accContextKey, acc)
  226. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  227. return test{
  228. auth: &mockAcmeAuthority{
  229. err: acme.ServerInternalErr(errors.New("force")),
  230. },
  231. ctx: ctx,
  232. statusCode: 500,
  233. problem: acme.ServerInternalErr(errors.New("force")),
  234. }
  235. },
  236. "ok": func(t *testing.T) test {
  237. acc := &acme.Account{ID: accID}
  238. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  239. ctx = context.WithValue(ctx, accContextKey, acc)
  240. ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
  241. return test{
  242. auth: &mockAcmeAuthority{
  243. getOrdersByAccount: func(p provisioner.Interface, id string) ([]string, error) {
  244. assert.Equals(t, p, prov)
  245. assert.Equals(t, id, acc.ID)
  246. return oids, nil
  247. },
  248. },
  249. ctx: ctx,
  250. statusCode: 200,
  251. }
  252. },
  253. }
  254. for name, run := range tests {
  255. tc := run(t)
  256. t.Run(name, func(t *testing.T) {
  257. h := New(tc.auth).(*Handler)
  258. req := httptest.NewRequest("GET", url, nil)
  259. req = req.WithContext(tc.ctx)
  260. w := httptest.NewRecorder()
  261. h.GetOrdersByAccount(w, req)
  262. res := w.Result()
  263. assert.Equals(t, res.StatusCode, tc.statusCode)
  264. body, err := ioutil.ReadAll(res.Body)
  265. res.Body.Close()
  266. assert.FatalError(t, err)
  267. if res.StatusCode >= 400 && assert.NotNil(t, tc.problem) {
  268. var ae acme.AError
  269. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  270. prob := tc.problem.ToACME()
  271. assert.Equals(t, ae.Type, prob.Type)
  272. assert.Equals(t, ae.Detail, prob.Detail)
  273. assert.Equals(t, ae.Identifier, prob.Identifier)
  274. assert.Equals(t, ae.Subproblems, prob.Subproblems)
  275. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  276. } else {
  277. expB, err := json.Marshal(oids)
  278. assert.FatalError(t, err)
  279. assert.Equals(t, bytes.TrimSpace(body), expB)
  280. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  281. }
  282. })
  283. }
  284. }
  285. func TestHandlerNewAccount(t *testing.T) {
  286. accID := "accountID"
  287. acc := acme.Account{
  288. ID: accID,
  289. Status: "valid",
  290. Orders: fmt.Sprintf("https://ca.smallstep.com/acme/account/%s/orders", accID),
  291. }
  292. prov := newProv()
  293. url := "https://ca.smallstep.com/acme/new-account"
  294. type test struct {
  295. auth acme.Interface
  296. ctx context.Context
  297. statusCode int
  298. problem *acme.Error
  299. }
  300. var tests = map[string]func(t *testing.T) test{
  301. "fail/no-provisioner": func(t *testing.T) test {
  302. return test{
  303. ctx: context.Background(),
  304. statusCode: 500,
  305. problem: acme.ServerInternalErr(errors.New("provisioner expected in request context")),
  306. }
  307. },
  308. "fail/nil-provisioner": func(t *testing.T) test {
  309. ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
  310. return test{
  311. ctx: ctx,
  312. statusCode: 500,
  313. problem: acme.ServerInternalErr(errors.New("provisioner expected in request context")),
  314. }
  315. },
  316. "fail/no-payload": func(t *testing.T) test {
  317. return test{
  318. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  319. statusCode: 500,
  320. problem: acme.ServerInternalErr(errors.New("payload expected in request context")),
  321. }
  322. },
  323. "fail/nil-payload": func(t *testing.T) test {
  324. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  325. ctx = context.WithValue(ctx, payloadContextKey, nil)
  326. return test{
  327. ctx: ctx,
  328. statusCode: 500,
  329. problem: acme.ServerInternalErr(errors.New("payload expected in request context")),
  330. }
  331. },
  332. "fail/unmarshal-payload-error": func(t *testing.T) test {
  333. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  334. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
  335. return test{
  336. ctx: ctx,
  337. statusCode: 400,
  338. problem: acme.MalformedErr(errors.New("failed to unmarshal new-account request payload: unexpected end of JSON input")),
  339. }
  340. },
  341. "fail/malformed-payload-error": func(t *testing.T) test {
  342. nar := &NewAccountRequest{
  343. Contact: []string{"foo", ""},
  344. }
  345. b, err := json.Marshal(nar)
  346. assert.FatalError(t, err)
  347. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  348. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  349. return test{
  350. ctx: ctx,
  351. statusCode: 400,
  352. problem: acme.MalformedErr(errors.New("contact cannot be empty string")),
  353. }
  354. },
  355. "fail/no-existing-account": func(t *testing.T) test {
  356. nar := &NewAccountRequest{
  357. OnlyReturnExisting: true,
  358. }
  359. b, err := json.Marshal(nar)
  360. assert.FatalError(t, err)
  361. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  362. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  363. return test{
  364. ctx: ctx,
  365. statusCode: 404,
  366. problem: acme.AccountDoesNotExistErr(nil),
  367. }
  368. },
  369. "fail/no-jwk": func(t *testing.T) test {
  370. nar := &NewAccountRequest{
  371. Contact: []string{"foo", "bar"},
  372. }
  373. b, err := json.Marshal(nar)
  374. assert.FatalError(t, err)
  375. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  376. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  377. return test{
  378. ctx: ctx,
  379. statusCode: 500,
  380. problem: acme.ServerInternalErr(errors.Errorf("jwk expected in request context")),
  381. }
  382. },
  383. "fail/nil-jwk": func(t *testing.T) test {
  384. nar := &NewAccountRequest{
  385. Contact: []string{"foo", "bar"},
  386. }
  387. b, err := json.Marshal(nar)
  388. assert.FatalError(t, err)
  389. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  390. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  391. ctx = context.WithValue(ctx, jwkContextKey, nil)
  392. return test{
  393. ctx: ctx,
  394. statusCode: 500,
  395. problem: acme.ServerInternalErr(errors.Errorf("jwk expected in request context")),
  396. }
  397. },
  398. "fail/NewAccount-error": func(t *testing.T) test {
  399. nar := &NewAccountRequest{
  400. Contact: []string{"foo", "bar"},
  401. }
  402. b, err := json.Marshal(nar)
  403. assert.FatalError(t, err)
  404. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  405. assert.FatalError(t, err)
  406. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  407. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  408. ctx = context.WithValue(ctx, jwkContextKey, jwk)
  409. return test{
  410. auth: &mockAcmeAuthority{
  411. newAccount: func(p provisioner.Interface, ops acme.AccountOptions) (*acme.Account, error) {
  412. assert.Equals(t, p, prov)
  413. assert.Equals(t, ops.Contact, nar.Contact)
  414. assert.Equals(t, ops.Key, jwk)
  415. return nil, acme.ServerInternalErr(errors.New("force"))
  416. },
  417. },
  418. ctx: ctx,
  419. statusCode: 500,
  420. problem: acme.ServerInternalErr(errors.New("force")),
  421. }
  422. },
  423. "ok/new-account": func(t *testing.T) test {
  424. nar := &NewAccountRequest{
  425. Contact: []string{"foo", "bar"},
  426. }
  427. b, err := json.Marshal(nar)
  428. assert.FatalError(t, err)
  429. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  430. assert.FatalError(t, err)
  431. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  432. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  433. ctx = context.WithValue(ctx, jwkContextKey, jwk)
  434. return test{
  435. auth: &mockAcmeAuthority{
  436. newAccount: func(p provisioner.Interface, ops acme.AccountOptions) (*acme.Account, error) {
  437. assert.Equals(t, p, prov)
  438. assert.Equals(t, ops.Contact, nar.Contact)
  439. assert.Equals(t, ops.Key, jwk)
  440. return &acc, nil
  441. },
  442. getLink: func(typ acme.Link, provID string, abs bool, in ...string) string {
  443. assert.Equals(t, provID, acme.URLSafeProvisionerName(prov))
  444. assert.Equals(t, typ, acme.AccountLink)
  445. assert.True(t, abs)
  446. assert.Equals(t, in, []string{accID})
  447. return fmt.Sprintf("https://ca.smallstep.com/acme/%s/account/%s",
  448. acme.URLSafeProvisionerName(prov), accID)
  449. },
  450. },
  451. ctx: ctx,
  452. statusCode: 201,
  453. }
  454. },
  455. "ok/return-existing": func(t *testing.T) test {
  456. nar := &NewAccountRequest{
  457. OnlyReturnExisting: true,
  458. }
  459. b, err := json.Marshal(nar)
  460. assert.FatalError(t, err)
  461. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  462. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  463. ctx = context.WithValue(ctx, accContextKey, &acc)
  464. return test{
  465. auth: &mockAcmeAuthority{
  466. getLink: func(typ acme.Link, provID string, abs bool, in ...string) string {
  467. assert.Equals(t, provID, acme.URLSafeProvisionerName(prov))
  468. assert.Equals(t, typ, acme.AccountLink)
  469. assert.True(t, abs)
  470. assert.Equals(t, in, []string{accID})
  471. return fmt.Sprintf("https://ca.smallstep.com/acme/%s/account/%s",
  472. acme.URLSafeProvisionerName(prov), accID)
  473. },
  474. },
  475. ctx: ctx,
  476. statusCode: 200,
  477. }
  478. },
  479. }
  480. for name, run := range tests {
  481. tc := run(t)
  482. t.Run(name, func(t *testing.T) {
  483. h := New(tc.auth).(*Handler)
  484. req := httptest.NewRequest("GET", url, nil)
  485. req = req.WithContext(tc.ctx)
  486. w := httptest.NewRecorder()
  487. h.NewAccount(w, req)
  488. res := w.Result()
  489. assert.Equals(t, res.StatusCode, tc.statusCode)
  490. body, err := ioutil.ReadAll(res.Body)
  491. res.Body.Close()
  492. assert.FatalError(t, err)
  493. if res.StatusCode >= 400 && assert.NotNil(t, tc.problem) {
  494. var ae acme.AError
  495. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  496. prob := tc.problem.ToACME()
  497. assert.Equals(t, ae.Type, prob.Type)
  498. assert.Equals(t, ae.Detail, prob.Detail)
  499. assert.Equals(t, ae.Identifier, prob.Identifier)
  500. assert.Equals(t, ae.Subproblems, prob.Subproblems)
  501. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  502. } else {
  503. expB, err := json.Marshal(acc)
  504. assert.FatalError(t, err)
  505. assert.Equals(t, bytes.TrimSpace(body), expB)
  506. assert.Equals(t, res.Header["Location"],
  507. []string{fmt.Sprintf("https://ca.smallstep.com/acme/%s/account/%s",
  508. acme.URLSafeProvisionerName(prov), accID)})
  509. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  510. }
  511. })
  512. }
  513. }
  514. func TestHandlerGetUpdateAccount(t *testing.T) {
  515. accID := "accountID"
  516. acc := acme.Account{
  517. ID: accID,
  518. Status: "valid",
  519. Orders: fmt.Sprintf("https://ca.smallstep.com/acme/account/%s/orders", accID),
  520. }
  521. prov := newProv()
  522. // Request with chi context
  523. url := fmt.Sprintf("http://ca.smallstep.com/acme/account/%s", accID)
  524. type test struct {
  525. auth acme.Interface
  526. ctx context.Context
  527. statusCode int
  528. problem *acme.Error
  529. }
  530. var tests = map[string]func(t *testing.T) test{
  531. "fail/no-provisioner": func(t *testing.T) test {
  532. return test{
  533. ctx: context.Background(),
  534. statusCode: 500,
  535. problem: acme.ServerInternalErr(errors.New("provisioner expected in request context")),
  536. }
  537. },
  538. "fail/nil-provisioner": func(t *testing.T) test {
  539. ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
  540. return test{
  541. ctx: ctx,
  542. statusCode: 500,
  543. problem: acme.ServerInternalErr(errors.New("provisioner expected in request context")),
  544. }
  545. },
  546. "fail/no-account": func(t *testing.T) test {
  547. return test{
  548. ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
  549. statusCode: 404,
  550. problem: acme.AccountDoesNotExistErr(nil),
  551. }
  552. },
  553. "fail/nil-account": func(t *testing.T) test {
  554. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  555. ctx = context.WithValue(ctx, accContextKey, nil)
  556. return test{
  557. ctx: ctx,
  558. statusCode: 404,
  559. problem: acme.AccountDoesNotExistErr(nil),
  560. }
  561. },
  562. "fail/no-payload": func(t *testing.T) test {
  563. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  564. ctx = context.WithValue(ctx, accContextKey, &acc)
  565. return test{
  566. ctx: ctx,
  567. statusCode: 500,
  568. problem: acme.ServerInternalErr(errors.New("payload expected in request context")),
  569. }
  570. },
  571. "fail/nil-payload": func(t *testing.T) test {
  572. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  573. ctx = context.WithValue(ctx, accContextKey, &acc)
  574. ctx = context.WithValue(ctx, payloadContextKey, nil)
  575. return test{
  576. ctx: ctx,
  577. statusCode: 500,
  578. problem: acme.ServerInternalErr(errors.New("payload expected in request context")),
  579. }
  580. },
  581. "fail/unmarshal-payload-error": func(t *testing.T) test {
  582. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  583. ctx = context.WithValue(ctx, accContextKey, &acc)
  584. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
  585. return test{
  586. ctx: ctx,
  587. statusCode: 400,
  588. problem: acme.MalformedErr(errors.New("failed to unmarshal new-account request payload: unexpected end of JSON input")),
  589. }
  590. },
  591. "fail/malformed-payload-error": func(t *testing.T) test {
  592. uar := &UpdateAccountRequest{
  593. Contact: []string{"foo", ""},
  594. }
  595. b, err := json.Marshal(uar)
  596. assert.FatalError(t, err)
  597. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  598. ctx = context.WithValue(ctx, accContextKey, &acc)
  599. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  600. return test{
  601. ctx: ctx,
  602. statusCode: 400,
  603. problem: acme.MalformedErr(errors.New("contact cannot be empty string")),
  604. }
  605. },
  606. "fail/Deactivate-error": func(t *testing.T) test {
  607. uar := &UpdateAccountRequest{
  608. Status: "deactivated",
  609. }
  610. b, err := json.Marshal(uar)
  611. assert.FatalError(t, err)
  612. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  613. ctx = context.WithValue(ctx, accContextKey, &acc)
  614. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  615. return test{
  616. auth: &mockAcmeAuthority{
  617. deactivateAccount: func(p provisioner.Interface, id string) (*acme.Account, error) {
  618. assert.Equals(t, p, prov)
  619. assert.Equals(t, id, accID)
  620. return nil, acme.ServerInternalErr(errors.New("force"))
  621. },
  622. },
  623. ctx: ctx,
  624. statusCode: 500,
  625. problem: acme.ServerInternalErr(errors.New("force")),
  626. }
  627. },
  628. "fail/UpdateAccount-error": func(t *testing.T) test {
  629. uar := &UpdateAccountRequest{
  630. Contact: []string{"foo", "bar"},
  631. }
  632. b, err := json.Marshal(uar)
  633. assert.FatalError(t, err)
  634. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  635. ctx = context.WithValue(ctx, accContextKey, &acc)
  636. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  637. return test{
  638. auth: &mockAcmeAuthority{
  639. updateAccount: func(p provisioner.Interface, id string, contacts []string) (*acme.Account, error) {
  640. assert.Equals(t, p, prov)
  641. assert.Equals(t, id, accID)
  642. assert.Equals(t, contacts, uar.Contact)
  643. return nil, acme.ServerInternalErr(errors.New("force"))
  644. },
  645. },
  646. ctx: ctx,
  647. statusCode: 500,
  648. problem: acme.ServerInternalErr(errors.New("force")),
  649. }
  650. },
  651. "ok/deactivate": func(t *testing.T) test {
  652. uar := &UpdateAccountRequest{
  653. Status: "deactivated",
  654. }
  655. b, err := json.Marshal(uar)
  656. assert.FatalError(t, err)
  657. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  658. ctx = context.WithValue(ctx, accContextKey, &acc)
  659. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  660. return test{
  661. auth: &mockAcmeAuthority{
  662. deactivateAccount: func(p provisioner.Interface, id string) (*acme.Account, error) {
  663. assert.Equals(t, p, prov)
  664. assert.Equals(t, id, accID)
  665. return &acc, nil
  666. },
  667. getLink: func(typ acme.Link, provID string, abs bool, in ...string) string {
  668. assert.Equals(t, typ, acme.AccountLink)
  669. assert.Equals(t, provID, acme.URLSafeProvisionerName(prov))
  670. assert.True(t, abs)
  671. assert.Equals(t, in, []string{accID})
  672. return fmt.Sprintf("https://ca.smallstep.com/acme/%s/account/%s",
  673. acme.URLSafeProvisionerName(prov), accID)
  674. },
  675. },
  676. ctx: ctx,
  677. statusCode: 200,
  678. }
  679. },
  680. "ok/new-account": func(t *testing.T) test {
  681. uar := &UpdateAccountRequest{
  682. Contact: []string{"foo", "bar"},
  683. }
  684. b, err := json.Marshal(uar)
  685. assert.FatalError(t, err)
  686. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  687. ctx = context.WithValue(ctx, accContextKey, &acc)
  688. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
  689. return test{
  690. auth: &mockAcmeAuthority{
  691. updateAccount: func(p provisioner.Interface, id string, contacts []string) (*acme.Account, error) {
  692. assert.Equals(t, p, prov)
  693. assert.Equals(t, id, accID)
  694. assert.Equals(t, contacts, uar.Contact)
  695. return &acc, nil
  696. },
  697. getLink: func(typ acme.Link, provID string, abs bool, in ...string) string {
  698. assert.Equals(t, typ, acme.AccountLink)
  699. assert.Equals(t, provID, acme.URLSafeProvisionerName(prov))
  700. assert.True(t, abs)
  701. assert.Equals(t, in, []string{accID})
  702. return fmt.Sprintf("https://ca.smallstep.com/acme/%s/account/%s",
  703. acme.URLSafeProvisionerName(prov), accID)
  704. },
  705. },
  706. ctx: ctx,
  707. statusCode: 200,
  708. }
  709. },
  710. "ok/post-as-get": func(t *testing.T) test {
  711. ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
  712. ctx = context.WithValue(ctx, accContextKey, &acc)
  713. ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{isPostAsGet: true})
  714. return test{
  715. auth: &mockAcmeAuthority{
  716. getLink: func(typ acme.Link, provID string, abs bool, in ...string) string {
  717. assert.Equals(t, typ, acme.AccountLink)
  718. assert.Equals(t, provID, acme.URLSafeProvisionerName(prov))
  719. assert.True(t, abs)
  720. assert.Equals(t, in, []string{accID})
  721. return fmt.Sprintf("https://ca.smallstep.com/acme/%s/account/%s",
  722. acme.URLSafeProvisionerName(prov), accID)
  723. },
  724. },
  725. ctx: ctx,
  726. statusCode: 200,
  727. }
  728. },
  729. }
  730. for name, run := range tests {
  731. tc := run(t)
  732. t.Run(name, func(t *testing.T) {
  733. h := New(tc.auth).(*Handler)
  734. req := httptest.NewRequest("GET", url, nil)
  735. req = req.WithContext(tc.ctx)
  736. w := httptest.NewRecorder()
  737. h.GetUpdateAccount(w, req)
  738. res := w.Result()
  739. assert.Equals(t, res.StatusCode, tc.statusCode)
  740. body, err := ioutil.ReadAll(res.Body)
  741. res.Body.Close()
  742. assert.FatalError(t, err)
  743. if res.StatusCode >= 400 && assert.NotNil(t, tc.problem) {
  744. var ae acme.AError
  745. assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
  746. prob := tc.problem.ToACME()
  747. assert.Equals(t, ae.Type, prob.Type)
  748. assert.Equals(t, ae.Detail, prob.Detail)
  749. assert.Equals(t, ae.Identifier, prob.Identifier)
  750. assert.Equals(t, ae.Subproblems, prob.Subproblems)
  751. assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
  752. } else {
  753. expB, err := json.Marshal(acc)
  754. assert.FatalError(t, err)
  755. assert.Equals(t, bytes.TrimSpace(body), expB)
  756. assert.Equals(t, res.Header["Location"],
  757. []string{fmt.Sprintf("https://ca.smallstep.com/acme/%s/account/%s",
  758. acme.URLSafeProvisionerName(prov), accID)})
  759. assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
  760. }
  761. })
  762. }
  763. }