Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Add support for the beta feature endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Shuhei Kitagawa committed Oct 11, 2018
1 parent 5e7e387 commit e3716ea
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
23 changes: 23 additions & 0 deletions beta_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,26 @@ func (bs *BetaFeaturesService) List(ctx context.Context, userId uint) ([]BetaFea

return getBetaFeaturesResponse.BetaFeatures, resp, err
}

// Update updates a user's beta_feature
//
// Travis CI API docs: https://developer.travis-ci.com/resource/beta_feature#update
func (bs *BetaFeaturesService) Update(ctx context.Context, userId uint, id uint, enabled bool) (*BetaFeature, *http.Response, error) {
u, err := urlWithOptions(fmt.Sprintf("/user/%d/beta_feature/%d", userId, id), nil)
if err != nil {
return nil, nil, err
}

req, err := bs.client.NewRequest(http.MethodPatch, u, map[string]bool{"enabled": enabled}, nil)
if err != nil {
return nil, nil, err
}

var feature BetaFeature
resp, err := bs.client.Do(ctx, req, &feature)
if err != nil {
return nil, resp, err
}

return &feature, resp, err
}
35 changes: 35 additions & 0 deletions beta_features_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import (
"context"
"net/http"
"testing"
"time"
)

const integrationBetaFeatureId = 1

func TestBetaFeaturesService_Integration_List(t *testing.T) {
_, res, err := integrationClient.BetaFeatures.List(context.TODO(), integrationUserId)

Expand All @@ -24,3 +27,35 @@ func TestBetaFeaturesService_Integration_List(t *testing.T) {
t.Fatalf("invalid http status: %s", res.Status)
}
}

func TestBetaFeaturesService_Integration_Update(t *testing.T) {
feature, res, err := integrationClient.BetaFeatures.Update(context.TODO(), integrationUserId, integrationBetaFeatureId, true)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("invalid http status: %s", res.Status)
}

if feature.Id != integrationBetaFeatureId || feature.Enabled != true {
t.Fatalf("unexpected beta feature has returned: %v", feature)
}

time.Sleep(2 * time.Second)

feature, res, err = integrationClient.BetaFeatures.Update(context.TODO(), integrationUserId, integrationBetaFeatureId, false)

if err != nil {
t.Fatalf("unexpected error occured: %s", err)
}

if res.StatusCode != http.StatusOK {
t.Fatalf("invalid http status: %s", res.Status)
}

if feature.Id != integrationBetaFeatureId || feature.Enabled != false {
t.Fatalf("unexpected beta feature has returned: %v", feature)
}
}
22 changes: 22 additions & 0 deletions beta_features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,25 @@ func TestBetaFeaturesService_List(t *testing.T) {
t.Errorf("BetaFeatures.List returned %+v, want %+v", features, want)
}
}

func TestBetaFeaturesService_Update(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc(fmt.Sprintf("/user/%d/beta_feature/1", testUserId), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPatch)
testBody(t, r, `{"enabled":true}`+"\n")
fmt.Fprint(w, `{"id":1,"name":"dashboard","description":"Try the new personal Dashboard layout","enabled":true,"feedback_url":"https://github.com/travis-ci/beta-features/issues/5"}`)
})

features, _, err := client.BetaFeatures.Update(context.Background(), testUserId, 1, true)

if err != nil {
t.Errorf("BetaFeatures.Update returned error: %v", err)
}

want := &BetaFeature{Id: 1, Name: "dashboard", Description: "Try the new personal Dashboard layout", Enabled: true, FeedbackUrl: "https://github.com/travis-ci/beta-features/issues/5"}
if !reflect.DeepEqual(features, want) {
t.Errorf("BetaFeatures.Update returned %+v, want %+v", features, want)
}
}

0 comments on commit e3716ea

Please sign in to comment.