account_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package acme
  2. import (
  3. "crypto"
  4. "encoding/base64"
  5. "testing"
  6. "time"
  7. "github.com/pkg/errors"
  8. "github.com/smallstep/assert"
  9. "go.step.sm/crypto/jose"
  10. )
  11. func TestKeyToID(t *testing.T) {
  12. type test struct {
  13. jwk *jose.JSONWebKey
  14. exp string
  15. err *Error
  16. }
  17. tests := map[string]func(t *testing.T) test{
  18. "fail/error-generating-thumbprint": func(t *testing.T) test {
  19. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  20. assert.FatalError(t, err)
  21. jwk.Key = "foo"
  22. return test{
  23. jwk: jwk,
  24. err: NewErrorISE("error generating jwk thumbprint: square/go-jose: unknown key type 'string'"),
  25. }
  26. },
  27. "ok": func(t *testing.T) test {
  28. jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
  29. assert.FatalError(t, err)
  30. kid, err := jwk.Thumbprint(crypto.SHA256)
  31. assert.FatalError(t, err)
  32. return test{
  33. jwk: jwk,
  34. exp: base64.RawURLEncoding.EncodeToString(kid),
  35. }
  36. },
  37. }
  38. for name, run := range tests {
  39. t.Run(name, func(t *testing.T) {
  40. tc := run(t)
  41. if id, err := KeyToID(tc.jwk); err != nil {
  42. if assert.NotNil(t, tc.err) {
  43. switch k := err.(type) {
  44. case *Error:
  45. assert.Equals(t, k.Type, tc.err.Type)
  46. assert.Equals(t, k.Detail, tc.err.Detail)
  47. assert.Equals(t, k.Status, tc.err.Status)
  48. assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
  49. assert.Equals(t, k.Detail, tc.err.Detail)
  50. default:
  51. assert.FatalError(t, errors.New("unexpected error type"))
  52. }
  53. }
  54. } else {
  55. if assert.Nil(t, tc.err) {
  56. assert.Equals(t, id, tc.exp)
  57. }
  58. }
  59. })
  60. }
  61. }
  62. func TestAccount_IsValid(t *testing.T) {
  63. type test struct {
  64. acc *Account
  65. exp bool
  66. }
  67. tests := map[string]test{
  68. "valid": {acc: &Account{Status: StatusValid}, exp: true},
  69. "invalid": {acc: &Account{Status: StatusInvalid}, exp: false},
  70. }
  71. for name, tc := range tests {
  72. t.Run(name, func(t *testing.T) {
  73. assert.Equals(t, tc.acc.IsValid(), tc.exp)
  74. })
  75. }
  76. }
  77. func TestExternalAccountKey_BindTo(t *testing.T) {
  78. boundAt := time.Now()
  79. tests := []struct {
  80. name string
  81. eak *ExternalAccountKey
  82. acct *Account
  83. err *Error
  84. }{
  85. {
  86. name: "ok",
  87. eak: &ExternalAccountKey{
  88. ID: "eakID",
  89. ProvisionerID: "provID",
  90. Reference: "ref",
  91. KeyBytes: []byte{1, 3, 3, 7},
  92. },
  93. acct: &Account{
  94. ID: "accountID",
  95. },
  96. err: nil,
  97. },
  98. {
  99. name: "fail/already-bound",
  100. eak: &ExternalAccountKey{
  101. ID: "eakID",
  102. ProvisionerID: "provID",
  103. Reference: "ref",
  104. KeyBytes: []byte{1, 3, 3, 7},
  105. AccountID: "someAccountID",
  106. BoundAt: boundAt,
  107. },
  108. acct: &Account{
  109. ID: "accountID",
  110. },
  111. err: NewError(ErrorUnauthorizedType, "external account binding key with id '%s' was already bound to account '%s' on %s", "eakID", "someAccountID", boundAt),
  112. },
  113. }
  114. for _, tt := range tests {
  115. t.Run(tt.name, func(t *testing.T) {
  116. eak := tt.eak
  117. acct := tt.acct
  118. err := eak.BindTo(acct)
  119. wantErr := tt.err != nil
  120. gotErr := err != nil
  121. if wantErr != gotErr {
  122. t.Errorf("ExternalAccountKey.BindTo() error = %v, wantErr %v", err, tt.err)
  123. }
  124. if wantErr {
  125. assert.NotNil(t, err)
  126. assert.Type(t, &Error{}, err)
  127. ae, _ := err.(*Error)
  128. assert.Equals(t, ae.Type, tt.err.Type)
  129. assert.Equals(t, ae.Detail, tt.err.Detail)
  130. assert.Equals(t, ae.Identifier, tt.err.Identifier)
  131. assert.Equals(t, ae.Subproblems, tt.err.Subproblems)
  132. } else {
  133. assert.Equals(t, eak.AccountID, acct.ID)
  134. assert.Equals(t, eak.KeyBytes, []byte{})
  135. assert.NotNil(t, eak.BoundAt)
  136. }
  137. })
  138. }
  139. }