forked from Silverfish/proton-bridge
GODT-35: Finish all details and make tests pass
This commit is contained in:
@ -44,7 +44,7 @@ func cleanup(client pmapi.Client, addresses *pmapi.AddressList) error {
|
||||
func cleanSystemFolders(client pmapi.Client) error {
|
||||
for _, labelID := range []string{pmapi.InboxLabel, pmapi.SentLabel, pmapi.ArchiveLabel, pmapi.AllMailLabel, pmapi.DraftLabel} {
|
||||
for {
|
||||
messages, total, err := client.ListMessages(context.TODO(), &pmapi.MessagesFilter{
|
||||
messages, total, err := client.ListMessages(context.Background(), &pmapi.MessagesFilter{
|
||||
PageSize: 150,
|
||||
LabelID: labelID,
|
||||
})
|
||||
@ -61,7 +61,7 @@ func cleanSystemFolders(client pmapi.Client) error {
|
||||
messageIDs = append(messageIDs, message.ID)
|
||||
}
|
||||
|
||||
if err := client.DeleteMessages(context.TODO(), messageIDs); err != nil {
|
||||
if err := client.DeleteMessages(context.Background(), messageIDs); err != nil {
|
||||
return errors.Wrap(err, "failed to delete messages")
|
||||
}
|
||||
|
||||
@ -74,7 +74,7 @@ func cleanSystemFolders(client pmapi.Client) error {
|
||||
}
|
||||
|
||||
func cleanCustomLables(client pmapi.Client) error {
|
||||
labels, err := client.ListLabels(context.TODO())
|
||||
labels, err := client.ListLabels(context.Background())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to list labels")
|
||||
}
|
||||
@ -83,7 +83,7 @@ func cleanCustomLables(client pmapi.Client) error {
|
||||
if err := emptyFolder(client, label.ID); err != nil {
|
||||
return errors.Wrap(err, "failed to empty label")
|
||||
}
|
||||
if err := client.DeleteLabel(context.TODO(), label.ID); err != nil {
|
||||
if err := client.DeleteLabel(context.Background(), label.ID); err != nil {
|
||||
return errors.Wrap(err, "failed to delete label")
|
||||
}
|
||||
}
|
||||
@ -93,7 +93,7 @@ func cleanCustomLables(client pmapi.Client) error {
|
||||
|
||||
func cleanTrash(client pmapi.Client) error {
|
||||
for {
|
||||
_, total, err := client.ListMessages(context.TODO(), &pmapi.MessagesFilter{
|
||||
_, total, err := client.ListMessages(context.Background(), &pmapi.MessagesFilter{
|
||||
PageSize: 1,
|
||||
LabelID: pmapi.TrashLabel,
|
||||
})
|
||||
@ -115,12 +115,12 @@ func cleanTrash(client pmapi.Client) error {
|
||||
}
|
||||
|
||||
func emptyFolder(client pmapi.Client, labelID string) error {
|
||||
err := client.EmptyFolder(context.TODO(), labelID, "")
|
||||
err := client.EmptyFolder(context.Background(), labelID, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for {
|
||||
_, total, err := client.ListMessages(context.TODO(), &pmapi.MessagesFilter{
|
||||
_, total, err := client.ListMessages(context.Background(), &pmapi.MessagesFilter{
|
||||
PageSize: 1,
|
||||
LabelID: labelID,
|
||||
})
|
||||
@ -142,5 +142,5 @@ func reorderAddresses(client pmapi.Client, addresses *pmapi.AddressList) error {
|
||||
addressIDs = append(addressIDs, address.ID)
|
||||
}
|
||||
|
||||
return client.ReorderAddresses(context.TODO(), addressIDs)
|
||||
return client.ReorderAddresses(context.Background(), addressIDs)
|
||||
}
|
||||
|
||||
@ -21,6 +21,7 @@ import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/ProtonMail/proton-bridge/internal/constants"
|
||||
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
|
||||
)
|
||||
|
||||
@ -36,19 +37,18 @@ type Controller struct {
|
||||
noInternetConnection bool
|
||||
}
|
||||
|
||||
func NewController() (*Controller, pmapi.Manager) {
|
||||
func NewController(app string) (*Controller, pmapi.Manager) {
|
||||
cm := pmapi.New(pmapi.NewConfig(getAppVersionName(app), constants.Version))
|
||||
controller := &Controller{
|
||||
lock: &sync.RWMutex{},
|
||||
calls: []*fakeCall{},
|
||||
pmapiByUsername: map[string]pmapi.Client{},
|
||||
messageIDsByUsername: map[string][]string{},
|
||||
clientManager: cm,
|
||||
|
||||
noInternetConnection: false,
|
||||
}
|
||||
|
||||
// FIXME(conman): Set connect values here?
|
||||
cm := pmapi.New(pmapi.DefaultConfig)
|
||||
|
||||
cm.SetTransport(&fakeTransport{
|
||||
ctl: controller,
|
||||
transport: http.DefaultTransport,
|
||||
@ -56,3 +56,10 @@ func NewController() (*Controller, pmapi.Manager) {
|
||||
|
||||
return controller, cm
|
||||
}
|
||||
|
||||
func getAppVersionName(app string) string {
|
||||
if app == "ie" {
|
||||
return "importExport"
|
||||
}
|
||||
return app
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@ func (ctl *Controller) AddUserLabel(username string, label *pmapi.Label) error {
|
||||
label.Exclusive = getLabelExclusive(label.Name)
|
||||
label.Name = getLabelNameWithoutPrefix(label.Name)
|
||||
label.Color = pmapi.LabelColors[0]
|
||||
if _, err := client.CreateLabel(context.TODO(), label); err != nil {
|
||||
if _, err := client.CreateLabel(context.Background(), label); err != nil {
|
||||
return errors.Wrap(err, "failed to create label")
|
||||
}
|
||||
return nil
|
||||
@ -73,7 +73,7 @@ func (ctl *Controller) getLabelID(username, labelName string) (string, error) {
|
||||
return "", fmt.Errorf("user %s does not exist", username)
|
||||
}
|
||||
|
||||
labels, err := client.ListLabels(context.TODO())
|
||||
labels, err := client.ListLabels(context.Background())
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to list labels")
|
||||
}
|
||||
@ -98,9 +98,6 @@ func getLabelNameWithoutPrefix(name string) string {
|
||||
return name
|
||||
}
|
||||
|
||||
func getLabelExclusive(name string) int {
|
||||
if strings.HasPrefix(name, "Folders/") {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
func getLabelExclusive(name string) pmapi.Boolean {
|
||||
return pmapi.Boolean(strings.HasPrefix(name, "Folders/"))
|
||||
}
|
||||
|
||||
@ -61,7 +61,7 @@ func (ctl *Controller) AddUserMessage(username string, message *pmapi.Message) (
|
||||
Message: body,
|
||||
}
|
||||
|
||||
results, err := client.Import(context.TODO(), pmapi.ImportMsgReqs{req})
|
||||
results, err := client.Import(context.Background(), pmapi.ImportMsgReqs{req})
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to make an import")
|
||||
}
|
||||
@ -85,7 +85,7 @@ func (ctl *Controller) GetMessages(username, labelID string) ([]*pmapi.Message,
|
||||
|
||||
for {
|
||||
// ListMessages returns empty result, not error, asking for page out of range.
|
||||
pageMessages, _, err := client.ListMessages(context.TODO(), &pmapi.MessagesFilter{
|
||||
pageMessages, _, err := client.ListMessages(context.Background(), &pmapi.MessagesFilter{
|
||||
Page: page,
|
||||
PageSize: 150,
|
||||
LabelID: labelID,
|
||||
|
||||
@ -48,9 +48,11 @@ func (t *fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to get body")
|
||||
}
|
||||
body, err = ioutil.ReadAll(bodyReader)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to read body")
|
||||
if bodyReader != nil {
|
||||
body, err = ioutil.ReadAll(bodyReader)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to read body")
|
||||
}
|
||||
}
|
||||
}
|
||||
t.ctl.recordCall(req.Method, req.URL.Path, body)
|
||||
|
||||
@ -30,12 +30,12 @@ func (ctl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList, p
|
||||
return godog.ErrPending
|
||||
}
|
||||
|
||||
client, _, err := ctl.clientManager.NewClientWithLogin(context.TODO(), user.Name, password)
|
||||
client, _, err := ctl.clientManager.NewClientWithLogin(context.Background(), user.Name, password)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create new client")
|
||||
}
|
||||
|
||||
salt, err := client.AuthSalt(context.TODO())
|
||||
salt, err := client.AuthSalt(context.Background())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get salt")
|
||||
}
|
||||
@ -45,7 +45,7 @@ func (ctl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList, p
|
||||
return errors.Wrap(err, "failed to hash mailbox password")
|
||||
}
|
||||
|
||||
if err := client.Unlock(context.TODO(), mailboxPassword); err != nil {
|
||||
if err := client.Unlock(context.Background(), mailboxPassword); err != nil {
|
||||
return errors.Wrap(err, "failed to unlock user")
|
||||
}
|
||||
|
||||
@ -59,5 +59,5 @@ func (ctl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList, p
|
||||
}
|
||||
|
||||
func (ctl *Controller) ReorderAddresses(user *pmapi.User, addressIDs []string) error {
|
||||
return ctl.pmapiByUsername[user.Name].ReorderAddresses(context.TODO(), addressIDs)
|
||||
return ctl.pmapiByUsername[user.Name].ReorderAddresses(context.Background(), addressIDs)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user