Skip to content

Commit

Permalink
Merge pull request #10 from bzimmer/zwift-test
Browse files Browse the repository at this point in the history
added zwift tests
  • Loading branch information
bzimmer authored Oct 18, 2021
2 parents 2e0d8f0 + c9aa869 commit 8dfc7da
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
# activity

[![build](https://github.com/bzimmer/activity/actions/workflows/build.yaml/badge.svg)](https://github.com/bzimmer/activity)
[![codecov](https://codecov.io/gh/bzimmer/activity/branch/main/graph/badge.svg?token=HIVK4NBVHR)](https://codecov.io/gh/bzimmer/activity)

Client libraries for activity-based services (Strava, Ride with GPS, Cycling Analytics, and more).
46 changes: 46 additions & 0 deletions zwift/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package zwift_test

import (
"context"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRefresh(t *testing.T) {
t.Parallel()
a := assert.New(t)

mux := http.NewServeMux()
mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
n, err := w.Write([]byte(`{
"access_token":"000aaabbbccc999",
"token_type":"bearer",
"expires_in":3600,
"refresh_token":"TotalNonsense",
"scope":"user"
}`))
a.Greater(n, 0)
a.NoError(err)
})

tests := []struct {
name string
}{
{
name: "success",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
client, svr := newClient(t, mux)
defer svr.Close()
token, err := client.Auth.Refresh(context.Background(), "foo", "bar")
a.NoError(err)
a.NotNil(token)
a.Equal("TotalNonsense", token.RefreshToken)
})
}
}
55 changes: 55 additions & 0 deletions zwift/profile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package zwift_test

import (
"context"
"encoding/json"
"net/http"
"testing"

"github.com/stretchr/testify/assert"

"github.com/bzimmer/activity/zwift"
)

func TestProfile(t *testing.T) {
t.Parallel()
a := assert.New(t)

mux := http.NewServeMux()
mux.HandleFunc("/api/profiles/abcxyz", func(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
a.NoError(enc.Encode(&zwift.Profile{FirstName: "barney"}))
})
mux.HandleFunc("/api/profiles/me", func(w http.ResponseWriter, r *http.Request) {
enc := json.NewEncoder(w)
a.NoError(enc.Encode(&zwift.Profile{FirstName: "betty"}))
})

tests := []struct {
name string
user string
firstname string
}{
{
name: "success me",
user: "",
firstname: "betty",
},
{
name: "success",
user: "abcxyz",
firstname: "barney",
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
client, svr := newClient(t, mux)
defer svr.Close()
profile, err := client.Profile.Profile(context.Background(), tt.user)
a.NoError(err)
a.NotNil(profile)
a.Equal(tt.firstname, profile.FirstName)
})
}
}
30 changes: 30 additions & 0 deletions zwift/zwift_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package zwift_test

import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/bzimmer/activity/zwift"
"github.com/stretchr/testify/assert"
"golang.org/x/oauth2"
)

func newClient(t *testing.T, mux *http.ServeMux) (*zwift.Client, *httptest.Server) {
a := assert.New(t)
svr := httptest.NewServer(mux)

endpoint := zwift.Endpoint()
endpoint.AuthURL = svr.URL + "/auth"
endpoint.TokenURL = svr.URL + "/token"

client, err := zwift.NewClient(
zwift.WithBaseURL(svr.URL),
zwift.WithConfig(oauth2.Config{Endpoint: endpoint}),
zwift.WithTokenCredentials("foo", "bar", time.Now().Add(time.Hour*24)),
zwift.WithClientCredentials("what", "now?"))
a.NoError(err)
a.NotNil(client)
return client, svr
}

0 comments on commit 8dfc7da

Please sign in to comment.