Skip to content

Commit 930d23b

Browse files
committed
Use bearer token again.
1 parent 264791a commit 930d23b

6 files changed

Lines changed: 23 additions & 102 deletions

File tree

‎.github/workflows/integrationtest.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ jobs:
1616
- name: run tests
1717
env:
1818
TEST_USER: ${{ secrets.SUNSYNK_USERNAME }}
19-
TEST_PASSWORD: ${{ secrets.SUNSYNK_PASSWORD }}
19+
TEST_BEARER_TOKEN: ${{ secrets.SUNSYNK_BEARER_TOKEN }}
2020
TEST_INVERTER_SN: ${{ secrets.SUNSYNK_INVERTER_SN }}
2121
run: go test -v -count=1 -tags integration ./integration

‎configuration/configuration.go‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Configuration struct {
3535
Endpoint string `yaml:"endpoint"`
3636
User string `yaml:"user"`
3737
Password string `yaml:"password"`
38+
Bearer string `yaml:"bearer"`
3839
DefaultInverterSN string `yaml:"default_inverter_sn,omitempty"`
3940
DefaultPlantID int `yaml:"default_plant_id,omitempty"`
4041
}
@@ -89,22 +90,22 @@ func ReadConfigurationFromFile(fileName string) (*Configuration, error) {
8990
return configuration, err
9091
}
9192

92-
// New constructs a configuration object with a specified username/password
93+
// New constructs a configuration object with a specified username/bearer token
9394
// combination using the default region 1 (London) endpoint
94-
func New(user string, password string) (*Configuration, error) {
95+
func New(user string, bearerToken string) (*Configuration, error) {
9596
if user == "" {
9697
return nil, errors.New("'user' cannot be empty")
9798
}
98-
if password == "" {
99+
if bearerToken == "" {
99100
return nil, errors.New("'password' cannot be empty")
100101
}
101-
return &Configuration{User: user, Password: password, Endpoint: DefaultEndpoint}, nil
102+
return &Configuration{User: user, Bearer: bearerToken, Endpoint: DefaultEndpoint}, nil
102103
}
103104

104-
// NewWithEndpoint constructs a configuration object with username/password credentials and an
105+
// NewWithEndpoint constructs a configuration object with username/bearer token credentials and an
105106
// overridden API endpoint
106-
func NewWithEndpoint(user string, password string, endpoint string) (*Configuration, error) {
107-
config, err := New(user, password)
107+
func NewWithEndpoint(user string, bearerToken string, endpoint string) (*Configuration, error) {
108+
config, err := New(user, bearerToken)
108109
if err != nil {
109110
return nil, err
110111
}

‎configuration/configuration_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ func TestWriteConfiguration(t *testing.T) {
116116
if err := yaml.Unmarshal(w.Bytes(), configMap); err != nil {
117117
t.Fatal("error:", err)
118118
}
119-
if len(configMap) != 3 {
120-
t.Errorf("expected 3 elements, got %d\n", len(configMap))
119+
if len(configMap) != 4 {
120+
t.Errorf("expected 4 elements, got %d\n", len(configMap))
121121
}
122122
if configuration.Endpoint != api {
123123
t.Errorf("expected Endpoint: %s, got %s", api, configuration.Endpoint)
@@ -155,8 +155,8 @@ func TestWriteConfigurationToFile(t *testing.T) {
155155
if err = yaml.Unmarshal(data, configMap); err != nil {
156156
t.Fatal("error: ", err)
157157
}
158-
if len(configMap) != 3 {
159-
t.Errorf("expected 3 elements, got %d\n", len(configMap))
158+
if len(configMap) != 4 {
159+
t.Errorf("expected 4 elements, got %d\n", len(configMap))
160160
}
161161
if configuration.Endpoint != api {
162162
t.Errorf("expected Endpoint: %s, got %s", api, configuration.Endpoint)

‎integration/integration_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ func getConfigFromVars() *configuration.Configuration {
3737
if username == "" {
3838
panic("the TEST_USER environment variable must be set")
3939
}
40-
password := os.Getenv("TEST_PASSWORD")
41-
if password == "" {
42-
panic("the TEST_PASSWORD environment variable must be set")
40+
bearerToken := os.Getenv("TEST_BEARER_TOKEN")
41+
if bearerToken == "" {
42+
panic("the TEST_BEARER_TOKEN environment variable must be set")
4343
}
44-
config, err := configuration.New(username, password)
44+
config, err := configuration.New(username, bearerToken)
4545
if err != nil {
4646
panic(err)
4747
}

‎rest/authentication.go‎

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"encoding/json"
2424
"io"
2525
"net/http"
26-
"net/url"
2726
"os"
2827

2928
"github.com/hammingweight/synkctl/configuration"
@@ -77,27 +76,11 @@ func init() {
7776
// Authenticate uses a specify configuration to authenticate a user. If successful, a SynkClient is
7877
// returned that can be used to make requests against the API.
7978
func Authenticate(ctx context.Context, config *configuration.Configuration) (*SynkClient, error) {
80-
url, err := url.JoinPath(config.Endpoint, "oauth", "token", "new")
81-
if err != nil {
82-
return nil, err
83-
}
84-
authRequest, err := newAuthRequestBody(config)
85-
if err != nil {
86-
return nil, err
87-
}
88-
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, authRequest)
89-
if err != nil {
90-
return nil, err
91-
}
92-
req.Header.Set("Content-Type", "application/json")
93-
req.Header.Set("Accept", "application/json")
94-
resp, err := http.DefaultClient.Do(req)
95-
if err != nil {
96-
return nil, err
97-
}
98-
tokens := &tokens{}
99-
if err = unmarshalResponseData(resp, tokens); err != nil {
100-
return nil, err
79+
tokens := &tokens{
80+
AccessToken: config.Bearer,
81+
ExpiresIn: 604799,
82+
TokenType: "Bearer",
83+
Scope: "all",
10184
}
102-
return &SynkClient{endpoint: config.Endpoint, tokens: *tokens, SerialNumber: config.DefaultInverterSN}, err
85+
return &SynkClient{endpoint: config.Endpoint, tokens: *tokens, SerialNumber: config.DefaultInverterSN}, nil
10386
}

‎rest/authentication_test.go‎

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,8 @@ limitations under the License.
1717
package rest
1818

1919
import (
20-
"context"
2120
"net/http"
2221
"net/http/httptest"
23-
"testing"
24-
25-
"github.com/hammingweight/synkctl/configuration"
2622
)
2723

2824
func mockServer(h http.HandlerFunc) (string, func()) {
@@ -32,62 +28,3 @@ func mockServer(h http.HandlerFunc) (string, func()) {
3228
ts.Close()
3329
}
3430
}
35-
36-
func TestAuthenticate(t *testing.T) {
37-
tests := []struct {
38-
Name string
39-
Resp struct {
40-
Code int
41-
Body string
42-
}
43-
ExpectErr bool
44-
AccessToken string
45-
}{{Name: "BadResponseCode",
46-
Resp: struct {
47-
Code int
48-
Body string
49-
}{404, ""},
50-
ExpectErr: true},
51-
{Name: "BadResponseBody",
52-
Resp: struct {
53-
Code int
54-
Body string
55-
}{200, "{this isn't json"},
56-
ExpectErr: true},
57-
{Name: "Unsuccessful",
58-
Resp: struct {
59-
Code int
60-
Body string
61-
}{200, `{"code":100, "msg":"this is a failure", "success":false, "data":{}}`},
62-
ExpectErr: true},
63-
{Name: "Successful",
64-
Resp: struct {
65-
Code int
66-
Body string
67-
}{200, `{"code":0, "msg":"", "success":true, "data":{"access_token":"12345"}}`},
68-
ExpectErr: false,
69-
AccessToken: "12345"},
70-
}
71-
for _, tc := range tests {
72-
t.Run(tc.Name, func(t *testing.T) {
73-
url, cleanup := mockServer(func(w http.ResponseWriter, r *http.Request) {
74-
w.WriteHeader(tc.Resp.Code)
75-
w.Write([]byte(tc.Resp.Body))
76-
})
77-
config := configuration.Configuration{Endpoint: url}
78-
client, err := Authenticate(context.Background(), &config)
79-
if tc.ExpectErr && err == nil {
80-
t.Error("Request should have failed")
81-
}
82-
if !tc.ExpectErr {
83-
if err != nil {
84-
t.Error("Request should have succeeded")
85-
}
86-
if tc.AccessToken != client.tokens.AccessToken {
87-
t.Errorf("Expected token %s, got %s\n", tc.AccessToken, client.tokens.AccessToken)
88-
}
89-
}
90-
defer cleanup()
91-
})
92-
}
93-
}

0 commit comments

Comments
 (0)