account_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package acme
  2. import (
  3. "crypto"
  4. "encoding/base64"
  5. "testing"
  6. "time"
  7. "github.com/pkg/errors"
  8. "go.step.sm/crypto/jose"
  9. "github.com/smallstep/assert"
  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: go-jose/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. var k *Error
  44. if errors.As(err, &k) {
  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. } else {
  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_GetLocation(t *testing.T) {
  63. locationPrefix := "https://test.ca.smallstep.com/acme/foo/account/"
  64. type test struct {
  65. acc *Account
  66. exp string
  67. }
  68. tests := map[string]test{
  69. "empty": {acc: &Account{LocationPrefix: ""}, exp: ""},
  70. "not-empty": {acc: &Account{ID: "bar", LocationPrefix: locationPrefix}, exp: locationPrefix + "bar"},
  71. }
  72. for name, tc := range tests {
  73. t.Run(name, func(t *testing.T) {
  74. assert.Equals(t, tc.acc.GetLocation(), tc.exp)
  75. })
  76. }
  77. }
  78. func TestAccount_IsValid(t *testing.T) {
  79. type test struct {
  80. acc *Account
  81. exp bool
  82. }
  83. tests := map[string]test{
  84. "valid": {acc: &Account{Status: StatusValid}, exp: true},
  85. "invalid": {acc: &Account{Status: StatusInvalid}, exp: false},
  86. }
  87. for name, tc := range tests {
  88. t.Run(name, func(t *testing.T) {
  89. assert.Equals(t, tc.acc.IsValid(), tc.exp)
  90. })
  91. }
  92. }
  93. func TestExternalAccountKey_BindTo(t *testing.T) {
  94. boundAt := time.Now()
  95. tests := []struct {
  96. name string
  97. eak *ExternalAccountKey
  98. acct *Account
  99. err *Error
  100. }{
  101. {
  102. name: "ok",
  103. eak: &ExternalAccountKey{
  104. ID: "eakID",
  105. ProvisionerID: "provID",
  106. Reference: "ref",
  107. HmacKey: []byte{1, 3, 3, 7},
  108. },
  109. acct: &Account{
  110. ID: "accountID",
  111. },
  112. err: nil,
  113. },
  114. {
  115. name: "fail/already-bound",
  116. eak: &ExternalAccountKey{
  117. ID: "eakID",
  118. ProvisionerID: "provID",
  119. Reference: "ref",
  120. HmacKey: []byte{1, 3, 3, 7},
  121. AccountID: "someAccountID",
  122. BoundAt: boundAt,
  123. },
  124. acct: &Account{
  125. ID: "accountID",
  126. },
  127. err: NewError(ErrorUnauthorizedType, "external account binding key with id '%s' was already bound to account '%s' on %s", "eakID", "someAccountID", boundAt),
  128. },
  129. }
  130. for _, tt := range tests {
  131. t.Run(tt.name, func(t *testing.T) {
  132. eak := tt.eak
  133. acct := tt.acct
  134. err := eak.BindTo(acct)
  135. wantErr := tt.err != nil
  136. gotErr := err != nil
  137. if wantErr != gotErr {
  138. t.Errorf("ExternalAccountKey.BindTo() error = %v, wantErr %v", err, tt.err)
  139. }
  140. if wantErr {
  141. assert.NotNil(t, err)
  142. var ae *Error
  143. if assert.True(t, errors.As(err, &ae)) {
  144. assert.Equals(t, ae.Type, tt.err.Type)
  145. assert.Equals(t, ae.Detail, tt.err.Detail)
  146. assert.Equals(t, ae.Subproblems, tt.err.Subproblems)
  147. }
  148. } else {
  149. assert.Equals(t, eak.AccountID, acct.ID)
  150. assert.Equals(t, eak.HmacKey, []byte{})
  151. assert.NotNil(t, eak.BoundAt)
  152. }
  153. })
  154. }
  155. }