|
| 1 | +package mittwaldv2_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/mittwald/api-client-go/mittwaldv2" |
| 6 | + "github.com/mittwald/api-client-go/mittwaldv2/generated/clients/user" |
| 7 | + "github.com/mittwald/api-client-go/pkg/httpclient_mock" |
| 8 | + . "github.com/onsi/ginkgo/v2" |
| 9 | + . "github.com/onsi/gomega" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | +) |
| 13 | + |
| 14 | +var _ = Describe("Client authentication", func() { |
| 15 | + Describe("WithAccessToken", func() { |
| 16 | + It("should append the provided access token to all requests", func() { |
| 17 | + ctx := context.Background() |
| 18 | + |
| 19 | + runner := &httpclient_mock.MockRequestRunner{} |
| 20 | + runner.ExpectRequest(http.MethodGet, "/v2/users/self/personal-information", httpclient_mock.WithJSONResponse(map[string]any{})) |
| 21 | + |
| 22 | + client, err := mittwaldv2.New(ctx, mittwaldv2.WithHTTPClient(runner), mittwaldv2.WithAccessToken("FOOBAR")) |
| 23 | + |
| 24 | + Expect(err).NotTo(HaveOccurred()) |
| 25 | + |
| 26 | + _, _, err = client.User().GetOwnAccount(ctx, user.GetOwnAccountRequest{}) |
| 27 | + |
| 28 | + Expect(err).NotTo(HaveOccurred()) |
| 29 | + Expect(runner.Requests).To(HaveLen(1)) |
| 30 | + Expect(runner.Requests[0].Header.Get("X-Access-Token")).To(Equal("FOOBAR")) |
| 31 | + }) |
| 32 | + }) |
| 33 | + |
| 34 | + Describe("WithAccessTokenFromEnv", func() { |
| 35 | + It("should retrieve the access token from the environment", func() { |
| 36 | + Expect(os.Setenv("MITTWALD_API_TOKEN", "FOOBAR")).To(Succeed()) |
| 37 | + |
| 38 | + ctx := context.Background() |
| 39 | + |
| 40 | + runner := &httpclient_mock.MockRequestRunner{} |
| 41 | + runner.ExpectRequest(http.MethodGet, "/v2/users/self/personal-information", httpclient_mock.WithJSONResponse(map[string]any{})) |
| 42 | + |
| 43 | + client, err := mittwaldv2.New(ctx, mittwaldv2.WithHTTPClient(runner), mittwaldv2.WithAccessTokenFromEnv()) |
| 44 | + |
| 45 | + Expect(err).NotTo(HaveOccurred()) |
| 46 | + |
| 47 | + _, _, err = client.User().GetOwnAccount(ctx, user.GetOwnAccountRequest{}) |
| 48 | + |
| 49 | + Expect(err).NotTo(HaveOccurred()) |
| 50 | + Expect(runner.Requests).To(HaveLen(1)) |
| 51 | + Expect(runner.Requests[0].Header.Get("X-Access-Token")).To(Equal("FOOBAR")) |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + Describe("WithUsernamePassword", func() { |
| 56 | + It("should retrieve the access token from an actual login", func() { |
| 57 | + ctx := context.Background() |
| 58 | + |
| 59 | + runner := &httpclient_mock.MockRequestRunner{} |
| 60 | + runner.ExpectRequest(http.MethodPost, "/v2/authenticate", httpclient_mock.WithJSONResponse(map[string]any{"token": "FOOBAR"})) |
| 61 | + runner.ExpectRequest(http.MethodGet, "/v2/users/self/personal-information", httpclient_mock.WithJSONResponse(map[string]any{})) |
| 62 | + |
| 63 | + client, err := mittwaldv2.New(ctx, mittwaldv2.WithHTTPClient(runner), mittwaldv2.WithUsernamePassword("martin@foo.example", "secret")) |
| 64 | + |
| 65 | + Expect(err).NotTo(HaveOccurred()) |
| 66 | + |
| 67 | + _, _, err = client.User().GetOwnAccount(ctx, user.GetOwnAccountRequest{}) |
| 68 | + |
| 69 | + Expect(err).NotTo(HaveOccurred()) |
| 70 | + Expect(runner.Requests).To(HaveLen(2)) |
| 71 | + Expect(runner.Requests[0].Header.Get("X-Access-Token")).To(Equal("")) |
| 72 | + Expect(runner.Requests[1].Header.Get("X-Access-Token")).To(Equal("FOOBAR")) |
| 73 | + }) |
| 74 | + |
| 75 | + It("should return an error when 2FA is required", func() { |
| 76 | + ctx := context.Background() |
| 77 | + |
| 78 | + runner := &httpclient_mock.MockRequestRunner{} |
| 79 | + runner.ExpectRequest( |
| 80 | + http.MethodPost, |
| 81 | + "/v2/authenticate", |
| 82 | + httpclient_mock.WithStatus(http.StatusAccepted), |
| 83 | + httpclient_mock.WithJSONResponse(map[string]any{"name": "SecondFactorRequired"})) |
| 84 | + |
| 85 | + _, err := mittwaldv2.New(ctx, mittwaldv2.WithHTTPClient(runner), mittwaldv2.WithUsernamePassword("martin@foo.example", "secret")) |
| 86 | + |
| 87 | + Expect(err).To(HaveOccurred()) |
| 88 | + Expect(err).To(MatchError("second factor required; use an API token instead")) |
| 89 | + }) |
| 90 | + }) |
| 91 | +}) |
0 commit comments