|
| 1 | +package buildkite |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/buildkite/go-buildkite/v4" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +type MockOrganizationsClient struct { |
| 13 | + ListFunc func(ctx context.Context, options *buildkite.OrganizationListOptions) ([]buildkite.Organization, *buildkite.Response, error) |
| 14 | +} |
| 15 | + |
| 16 | +func (m *MockOrganizationsClient) List(ctx context.Context, options *buildkite.OrganizationListOptions) ([]buildkite.Organization, *buildkite.Response, error) { |
| 17 | + if m.ListFunc != nil { |
| 18 | + return m.ListFunc(ctx, options) |
| 19 | + } |
| 20 | + return nil, nil, nil |
| 21 | +} |
| 22 | + |
| 23 | +func TestUserTokenOrganization(t *testing.T) { |
| 24 | + assert := require.New(t) |
| 25 | + |
| 26 | + ctx := context.Background() |
| 27 | + client := &MockOrganizationsClient{ |
| 28 | + ListFunc: func(ctx context.Context, options *buildkite.OrganizationListOptions) ([]buildkite.Organization, *buildkite.Response, error) { |
| 29 | + return []buildkite.Organization{ |
| 30 | + { |
| 31 | + Slug: "test-org", |
| 32 | + Name: "Test Organization", |
| 33 | + }, |
| 34 | + }, &buildkite.Response{ |
| 35 | + Response: &http.Response{ |
| 36 | + StatusCode: 200, |
| 37 | + }, |
| 38 | + }, nil |
| 39 | + }, |
| 40 | + } |
| 41 | + |
| 42 | + tool, handler := UserTokenOrganization(ctx, client) |
| 43 | + assert.NotNil(tool) |
| 44 | + assert.NotNil(handler) |
| 45 | + |
| 46 | + request := createMCPRequest(t, map[string]any{}) |
| 47 | + result, err := handler(ctx, request) |
| 48 | + assert.NoError(err) |
| 49 | + |
| 50 | + textContent := getTextResult(t, result) |
| 51 | + |
| 52 | + assert.Equal(`{"name":"Test Organization","slug":"test-org"}`, textContent.Text) |
| 53 | +} |
| 54 | + |
| 55 | +func TestUserTokenOrganizationError(t *testing.T) { |
| 56 | + assert := require.New(t) |
| 57 | + |
| 58 | + ctx := context.Background() |
| 59 | + client := &MockOrganizationsClient{ |
| 60 | + ListFunc: func(ctx context.Context, options *buildkite.OrganizationListOptions) ([]buildkite.Organization, *buildkite.Response, error) { |
| 61 | + return nil, &buildkite.Response{ |
| 62 | + Response: &http.Response{ |
| 63 | + StatusCode: 500, |
| 64 | + }, |
| 65 | + }, nil |
| 66 | + }, |
| 67 | + } |
| 68 | + |
| 69 | + tool, handler := UserTokenOrganization(ctx, client) |
| 70 | + assert.NotNil(tool) |
| 71 | + assert.NotNil(handler) |
| 72 | + |
| 73 | + request := createMCPRequest(t, map[string]any{}) |
| 74 | + result, err := handler(ctx, request) |
| 75 | + assert.NoError(err) |
| 76 | + |
| 77 | + textContent := getTextResult(t, result) |
| 78 | + |
| 79 | + assert.Equal("failed to get current user organizations", textContent.Text) |
| 80 | +} |
| 81 | + |
| 82 | +func TestUserTokenOrganizationErrorNoOrganization(t *testing.T) { |
| 83 | + assert := require.New(t) |
| 84 | + |
| 85 | + ctx := context.Background() |
| 86 | + client := &MockOrganizationsClient{ |
| 87 | + ListFunc: func(ctx context.Context, options *buildkite.OrganizationListOptions) ([]buildkite.Organization, *buildkite.Response, error) { |
| 88 | + return nil, &buildkite.Response{ |
| 89 | + Response: &http.Response{ |
| 90 | + StatusCode: 200, |
| 91 | + }, |
| 92 | + }, nil |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + tool, handler := UserTokenOrganization(ctx, client) |
| 97 | + assert.NotNil(tool) |
| 98 | + assert.NotNil(handler) |
| 99 | + |
| 100 | + request := createMCPRequest(t, map[string]any{}) |
| 101 | + result, err := handler(ctx, request) |
| 102 | + assert.NoError(err) |
| 103 | + |
| 104 | + textContent := getTextResult(t, result) |
| 105 | + |
| 106 | + assert.Equal("no organization found for the current user token", textContent.Text) |
| 107 | +} |
0 commit comments