test: fix most integration tests (live)

This commit is contained in:
James Houlahan
2020-04-09 10:24:58 +02:00
parent bafd4e714e
commit fec5f2d3c3
18 changed files with 124 additions and 104 deletions

View File

@ -29,20 +29,20 @@ type fakeCall struct {
request []byte
}
func (cntrl *Controller) recordCall(method, path string, request []byte) {
cntrl.lock.Lock()
defer cntrl.lock.Unlock()
func (ctl *Controller) recordCall(method, path string, request []byte) {
ctl.lock.Lock()
defer ctl.lock.Unlock()
cntrl.calls = append(cntrl.calls, &fakeCall{
ctl.calls = append(ctl.calls, &fakeCall{
method: method,
path: path,
request: request,
})
}
func (cntrl *Controller) PrintCalls() {
func (ctl *Controller) PrintCalls() {
fmt.Println("API calls:")
for idx, call := range cntrl.calls {
for idx, call := range ctl.calls {
fmt.Printf("%02d: [%s] %s\n", idx+1, call.method, call.path)
if call.request != nil && string(call.request) != "null" {
fmt.Printf("\t%s\n", call.request)
@ -50,8 +50,8 @@ func (cntrl *Controller) PrintCalls() {
}
}
func (cntrl *Controller) WasCalled(method, path string, expectedRequest []byte) bool {
for _, call := range cntrl.calls {
func (ctl *Controller) WasCalled(method, path string, expectedRequest []byte) bool {
for _, call := range ctl.calls {
if call.method != method && call.path != path {
continue
}
@ -64,9 +64,9 @@ func (cntrl *Controller) WasCalled(method, path string, expectedRequest []byte)
return false
}
func (cntrl *Controller) GetCalls(method, path string) [][]byte {
func (ctl *Controller) GetCalls(method, path string) [][]byte {
requests := [][]byte{}
for _, call := range cntrl.calls {
for _, call := range ctl.calls {
if call.method == method && call.path == path {
requests = append(requests, call.request)
}

View File

@ -28,6 +28,7 @@ type Controller struct {
// Internal states.
lock *sync.RWMutex
calls []*fakeCall
pmapiByUsername map[string]pmapi.Client
messageIDsByUsername map[string][]string
clientManager *pmapi.ClientManager
@ -35,10 +36,11 @@ type Controller struct {
noInternetConnection bool
}
func NewController(cm *pmapi.ClientManager) (cntrl *Controller) {
cntrl = &Controller{
func NewController(cm *pmapi.ClientManager) *Controller {
controller := &Controller{
lock: &sync.RWMutex{},
calls: []*fakeCall{},
pmapiByUsername: map[string]pmapi.Client{},
messageIDsByUsername: map[string][]string{},
clientManager: cm,
@ -46,9 +48,9 @@ func NewController(cm *pmapi.ClientManager) (cntrl *Controller) {
}
cm.SetRoundTripper(&fakeTransport{
cntrl: cntrl,
ctl: controller,
transport: http.DefaultTransport,
})
return
return controller
}

View File

@ -35,8 +35,12 @@ var systemLabelNameToID = map[string]string{ //nolint[gochecknoglobals]
"Drafts": pmapi.DraftLabel,
}
func (cntrl *Controller) AddUserLabel(username string, label *pmapi.Label) error {
client := cntrl.clientManager.GetClient(username)
func (ctl *Controller) AddUserLabel(username string, label *pmapi.Label) error {
client, ok := ctl.pmapiByUsername[username]
if !ok {
return fmt.Errorf("user %s does not exist", username)
}
label.Exclusive = getLabelExclusive(label.Name)
label.Name = getLabelNameWithoutPrefix(label.Name)
label.Color = pmapi.LabelColors[0]
@ -46,10 +50,10 @@ func (cntrl *Controller) AddUserLabel(username string, label *pmapi.Label) error
return nil
}
func (cntrl *Controller) GetLabelIDs(username string, labelNames []string) ([]string, error) {
func (ctl *Controller) GetLabelIDs(username string, labelNames []string) ([]string, error) {
labelIDs := []string{}
for _, labelName := range labelNames {
labelID, err := cntrl.getLabelID(username, labelName)
labelID, err := ctl.getLabelID(username, labelName)
if err != nil {
return nil, err
}
@ -58,12 +62,16 @@ func (cntrl *Controller) GetLabelIDs(username string, labelNames []string) ([]st
return labelIDs, nil
}
func (cntrl *Controller) getLabelID(username, labelName string) (string, error) {
func (ctl *Controller) getLabelID(username, labelName string) (string, error) {
if labelID, ok := systemLabelNameToID[labelName]; ok {
return labelID, nil
}
client := cntrl.clientManager.GetClient(username)
client, ok := ctl.pmapiByUsername[username]
if !ok {
return "", fmt.Errorf("user %s does not exist", username)
}
labels, err := client.ListLabels()
if err != nil {
return "", errors.Wrap(err, "failed to list labels")

View File

@ -30,8 +30,11 @@ import (
"github.com/pkg/errors"
)
func (cntrl *Controller) AddUserMessage(username string, message *pmapi.Message) error {
client := cntrl.clientManager.GetClient(username)
func (ctl *Controller) AddUserMessage(username string, message *pmapi.Message) error {
client, ok := ctl.pmapiByUsername[username]
if !ok {
return fmt.Errorf("user %s does not exist", username)
}
body, err := buildMessage(client, message)
if err != nil {
@ -55,7 +58,7 @@ func (cntrl *Controller) AddUserMessage(username string, message *pmapi.Message)
if result.Error != nil {
return errors.Wrap(result.Error, "failed to import message")
}
cntrl.messageIDsByUsername[username] = append(cntrl.messageIDsByUsername[username], result.MessageID)
ctl.messageIDsByUsername[username] = append(ctl.messageIDsByUsername[username], result.MessageID)
}
return nil
@ -122,10 +125,10 @@ func buildMessageBody(message *pmapi.Message, body *bytes.Buffer) error {
return nil
}
func (cntrl *Controller) GetMessageID(username, messageIndex string) string {
func (ctl *Controller) GetMessageID(username, messageIndex string) string {
idx, err := strconv.Atoi(messageIndex)
if err != nil {
panic(fmt.Sprintf("message index %s not found", messageIndex))
}
return cntrl.messageIDsByUsername[username][idx-1]
return ctl.messageIDsByUsername[username][idx-1]
}

View File

@ -24,21 +24,21 @@ import (
"github.com/pkg/errors"
)
func (cntrl *Controller) TurnInternetConnectionOff() {
cntrl.noInternetConnection = true
func (ctl *Controller) TurnInternetConnectionOff() {
ctl.noInternetConnection = true
}
func (cntrl *Controller) TurnInternetConnectionOn() {
cntrl.noInternetConnection = false
func (ctl *Controller) TurnInternetConnectionOn() {
ctl.noInternetConnection = false
}
type fakeTransport struct {
cntrl *Controller
ctl *Controller
transport http.RoundTripper
}
func (t *fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
if t.cntrl.noInternetConnection {
if t.ctl.noInternetConnection {
return nil, errors.New("no route to host")
}
@ -53,7 +53,7 @@ func (t *fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return nil, errors.Wrap(err, "failed to read body")
}
}
t.cntrl.recordCall(req.Method, req.URL.Path, body)
t.ctl.recordCall(req.Method, req.URL.Path, body)
return t.transport.RoundTrip(req)
}

View File

@ -23,12 +23,12 @@ import (
"github.com/pkg/errors"
)
func (cntrl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList, password string, twoFAEnabled bool) error {
func (ctl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList, password string, twoFAEnabled bool) error {
if twoFAEnabled {
return godog.ErrPending
}
client := cntrl.clientManager.GetClient(user.ID)
client := ctl.clientManager.GetClient(user.ID)
authInfo, err := client.AuthInfo(user.Name)
if err != nil {
@ -54,5 +54,7 @@ func (cntrl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList,
return errors.Wrap(err, "failed to clean user")
}
ctl.pmapiByUsername[user.Name] = client
return nil
}