bitwarden_sdk_test.go 4.4 KB

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