Add SetToken function to NewAPIv4Client (#11722)
Этот коммит содержится в:
коммит произвёл
Ben Schumacher
родитель
bd270aecbd
Коммит
8cdbc734b9
@@ -208,7 +208,7 @@ func (me *LoadTestProvider) SetupCommand(a *App, args *model.CommandArgs, messag
|
||||
return &model.CommandResponse{Text: "Failed to create testing environment", ResponseType: model.COMMAND_RESPONSE_TYPE_EPHEMERAL}
|
||||
}
|
||||
|
||||
client.MockSession(args.Session.Token)
|
||||
client.SetToken(args.Session.Token)
|
||||
CreateTestEnvironmentInTeam(
|
||||
a,
|
||||
client,
|
||||
@@ -269,7 +269,7 @@ func (me *LoadTestProvider) ChannelsCommand(a *App, args *model.CommandArgs, mes
|
||||
}
|
||||
|
||||
client := model.NewAPIv4Client(args.SiteURL)
|
||||
client.MockSession(args.Session.Token)
|
||||
client.SetToken(args.Session.Token)
|
||||
channelCreator := NewAutoChannelCreator(client, team)
|
||||
channelCreator.Fuzzy = doFuzz
|
||||
channelCreator.CreateTestChannels(channelsr)
|
||||
@@ -311,7 +311,7 @@ func (me *LoadTestProvider) PostsCommand(a *App, args *model.CommandArgs, messag
|
||||
}
|
||||
|
||||
client := model.NewAPIv4Client(args.SiteURL)
|
||||
client.MockSession(args.Session.Token)
|
||||
client.SetToken(args.Session.Token)
|
||||
testPoster := NewAutoPostCreator(client, args.ChannelId)
|
||||
testPoster.Fuzzy = doFuzz
|
||||
testPoster.Users = usernames
|
||||
|
||||
@@ -112,11 +112,16 @@ func BuildResponse(r *http.Response) *Response {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client4) MockSession(sessionToken string) {
|
||||
c.AuthToken = sessionToken
|
||||
func (c *Client4) SetToken(token string) {
|
||||
c.AuthToken = token
|
||||
c.AuthType = HEADER_BEARER
|
||||
}
|
||||
|
||||
// MockSession is deprecated in favour of SetToken
|
||||
func (c *Client4) MockSession(token string) {
|
||||
c.SetToken(token)
|
||||
}
|
||||
|
||||
func (c *Client4) SetOAuthToken(token string) {
|
||||
c.AuthToken = token
|
||||
c.AuthType = HEADER_TOKEN
|
||||
|
||||
@@ -6,6 +6,7 @@ package model
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -56,3 +57,47 @@ func TestClient4CreatePost(t *testing.T) {
|
||||
_, resp := client.CreatePost(post)
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestClient4SetToken(t *testing.T) {
|
||||
expected := NewId()
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
authHeader := r.Header.Get(HEADER_AUTH)
|
||||
|
||||
token := strings.Split(authHeader, HEADER_BEARER)
|
||||
|
||||
if len(token) < 2 {
|
||||
t.Errorf("wrong authorization header format, got %s, expected: %s %s", authHeader, HEADER_BEARER, expected)
|
||||
}
|
||||
|
||||
assert.Equal(t, expected, strings.TrimSpace(token[1]))
|
||||
}))
|
||||
|
||||
client := NewAPIv4Client(server.URL)
|
||||
client.SetToken(expected)
|
||||
|
||||
_, resp := client.GetMe("")
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
func TestClient4MockSession(t *testing.T) {
|
||||
expected := NewId()
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
authHeader := r.Header.Get(HEADER_AUTH)
|
||||
|
||||
token := strings.Split(authHeader, HEADER_BEARER)
|
||||
|
||||
if len(token) < 2 {
|
||||
t.Errorf("wrong authorization header format, got %s, expected: %s %s", authHeader, HEADER_BEARER, expected)
|
||||
}
|
||||
|
||||
assert.Equal(t, expected, strings.TrimSpace(token[1]))
|
||||
}))
|
||||
|
||||
client := NewAPIv4Client(server.URL)
|
||||
client.MockSession(expected)
|
||||
|
||||
_, resp := client.GetMe("")
|
||||
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user