bitwarden_sdk_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package bitwarden
  13. import (
  14. "context"
  15. "encoding/json"
  16. "net/http"
  17. "net/http/httptest"
  18. "reflect"
  19. "testing"
  20. )
  21. // The rest of the tests much look the same, it would be nice if I could find a way
  22. // to nicely unify the tests for all of them.
  23. func TestSdkClient_CreateSecret(t *testing.T) {
  24. type fields struct {
  25. apiURL func(c *httptest.Server) string
  26. identityURL func(c *httptest.Server) string
  27. bitwardenSdkServerURL func(c *httptest.Server) string
  28. token string
  29. testServer func(response any) *httptest.Server
  30. response any
  31. }
  32. type args struct {
  33. ctx context.Context
  34. createReq SecretCreateRequest
  35. }
  36. tests := []struct {
  37. name string
  38. fields fields
  39. args args
  40. want *SecretResponse
  41. wantErr bool
  42. }{
  43. {
  44. name: "create secret is successful",
  45. fields: fields{
  46. testServer: func(response any) *httptest.Server {
  47. testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  48. data, err := json.Marshal(response)
  49. if err != nil {
  50. http.Error(w, err.Error(), http.StatusInternalServerError)
  51. return
  52. }
  53. w.Write(data)
  54. }))
  55. return testServer
  56. },
  57. apiURL: func(c *httptest.Server) string {
  58. return c.URL
  59. },
  60. identityURL: func(c *httptest.Server) string {
  61. return c.URL
  62. },
  63. bitwardenSdkServerURL: func(c *httptest.Server) string {
  64. return c.URL
  65. },
  66. token: "token",
  67. response: &SecretResponse{
  68. ID: "id",
  69. Key: "key",
  70. Note: "note",
  71. OrganizationID: "orgID",
  72. RevisionDate: "2024-04-04",
  73. Value: "value",
  74. },
  75. },
  76. args: args{
  77. ctx: context.Background(),
  78. createReq: SecretCreateRequest{
  79. Key: "key",
  80. Note: "note",
  81. OrganizationID: "orgID",
  82. ProjectIDS: []string{projectID},
  83. Value: "value",
  84. },
  85. },
  86. want: &SecretResponse{
  87. ID: "id",
  88. Key: "key",
  89. Note: "note",
  90. OrganizationID: "orgID",
  91. RevisionDate: "2024-04-04",
  92. Value: "value",
  93. },
  94. },
  95. {
  96. name: "create secret fails",
  97. fields: fields{
  98. testServer: func(response any) *httptest.Server {
  99. testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  100. http.Error(w, "nope", http.StatusInternalServerError)
  101. }))
  102. return testServer
  103. },
  104. apiURL: func(c *httptest.Server) string {
  105. return c.URL
  106. },
  107. identityURL: func(c *httptest.Server) string {
  108. return c.URL
  109. },
  110. bitwardenSdkServerURL: func(c *httptest.Server) string {
  111. return c.URL
  112. },
  113. token: "token",
  114. response: &SecretResponse{
  115. ID: "id",
  116. Key: "key",
  117. Note: "note",
  118. OrganizationID: "orgID",
  119. RevisionDate: "2024-04-04",
  120. Value: "value",
  121. },
  122. },
  123. args: args{
  124. ctx: context.Background(),
  125. createReq: SecretCreateRequest{
  126. Key: "key",
  127. Note: "note",
  128. OrganizationID: "orgID",
  129. ProjectIDS: []string{projectID},
  130. Value: "value",
  131. },
  132. },
  133. wantErr: true,
  134. },
  135. }
  136. for _, tt := range tests {
  137. t.Run(tt.name, func(t *testing.T) {
  138. server := tt.fields.testServer(tt.fields.response)
  139. defer server.Close()
  140. s := &SdkClient{
  141. apiURL: tt.fields.apiURL(server),
  142. identityURL: tt.fields.identityURL(server),
  143. bitwardenSdkServerURL: tt.fields.bitwardenSdkServerURL(server),
  144. token: tt.fields.token,
  145. client: server.Client(),
  146. }
  147. got, err := s.CreateSecret(tt.args.ctx, tt.args.createReq)
  148. if (err != nil) != tt.wantErr {
  149. t.Errorf("CreateSecret() error = %v, wantErr %v", err, tt.wantErr)
  150. return
  151. }
  152. if !reflect.DeepEqual(got, tt.want) {
  153. t.Errorf("CreateSecret() got = %v, want %v", got, tt.want)
  154. }
  155. })
  156. }
  157. }