diff --git a/.gitignore b/.gitignore index c581686..7d5aef3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ # Editor files .*.sw? *~ +.idea # Test files godog.test @@ -29,3 +30,4 @@ vendor-cache /hasher cmd/Desktop-Bridge/deploy cmd/Import-Export/deploy +proton-bridge diff --git a/go.mod b/go.mod index 958399f..f20fc57 100644 --- a/go.mod +++ b/go.mod @@ -7,15 +7,15 @@ go 1.15 require ( github.com/emersion/go-imap v1.0.6 github.com/emersion/go-message v0.12.1-0.20201221184100-40c3f864532b - github.com/jameskeane/bcrypt v0.0.0-20170924085257-7509ea014998 // indirect ) require ( - github.com/ProtonMail/go-crypto v0.0.0-20211221144345-a4f6767435ab + github.com/ProtonMail/bcrypt v0.0.0-20211005172633-e235017c1baf // indirect + github.com/ProtonMail/go-crypto v0.0.0-20220623141421-5afb4c282135 github.com/ProtonMail/go-rfc5322 v0.8.0 - github.com/ProtonMail/go-srp v0.0.1 + github.com/ProtonMail/go-srp v0.0.5 github.com/ProtonMail/go-vcard v0.0.0-20180326232728-33aaa0a0c8a5 - github.com/ProtonMail/gopenpgp/v2 v2.4.1 + github.com/ProtonMail/gopenpgp/v2 v2.4.7 github.com/PuerkitoBio/goquery v1.5.1 github.com/emersion/go-imap-appendlimit v0.0.0-20190308131241-25671c986a6a github.com/emersion/go-imap-move v0.0.0-20190710073258-6e5a51a5b342 @@ -43,14 +43,14 @@ require ( github.com/stretchr/testify v1.7.0 github.com/vmihailenco/msgpack/v5 v5.1.3 go.etcd.io/bbolt v1.3.6 - golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 + golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 - golang.org/x/sys v0.0.0-20220111092808-5a964db01320 // indirect + golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect golang.org/x/text v0.3.7 ) replace ( github.com/emersion/go-imap => github.com/ProtonMail/go-imap v0.0.0-20201228133358-4db68cea0cac github.com/emersion/go-message => github.com/ProtonMail/go-message v0.0.0-20210611055058-fabeff2ec753 - github.com/jameskeane/bcrypt => github.com/ProtonMail/bcrypt v0.0.0-20210511135022-227b4adcab57 + github.com/keybase/go-keychain => github.com/cuthix/go-keychain v0.0.0-20220405075754-31e7cee908fe ) diff --git a/go.sum b/go.sum index 1725314..11bcef7 100644 --- a/go.sum +++ b/go.sum @@ -1,31 +1,33 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/ProtonMail/bcrypt v0.0.0-20210511135022-227b4adcab57 h1:pHA4K54ifoogVLunGGHi3xyF5Nz4x+Uh3dJuy3NwGQQ= github.com/ProtonMail/bcrypt v0.0.0-20210511135022-227b4adcab57/go.mod h1:HecWFHognK8GfRDGnFQbW/LiV7A3MX3gZVs45vk5h8I= +github.com/ProtonMail/bcrypt v0.0.0-20211005172633-e235017c1baf h1:yc9daCCYUefEs69zUkSzubzjBbL+cmOXgnmt9Fyd9ug= +github.com/ProtonMail/bcrypt v0.0.0-20211005172633-e235017c1baf/go.mod h1:o0ESU9p83twszAU8LBeJKFAAMX14tISa0yk4Oo5TOqo= github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= -github.com/ProtonMail/go-crypto v0.0.0-20211221144345-a4f6767435ab h1:5FiL/TCaiKCss/BLMIACDxxadYrx767l9kh0qYX+sLQ= -github.com/ProtonMail/go-crypto v0.0.0-20211221144345-a4f6767435ab/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= +github.com/ProtonMail/go-crypto v0.0.0-20220113124808-70ae35bab23f/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= +github.com/ProtonMail/go-crypto v0.0.0-20220623141421-5afb4c282135 h1:xDc/cFH/hwyr9KyWc0sm26lpsscqtfZBvU8NpRLHwJ0= +github.com/ProtonMail/go-crypto v0.0.0-20220623141421-5afb4c282135/go.mod h1:z4/9nQmJSSwwds7ejkxaJwO37dru3geImFUdJlaLzQo= github.com/ProtonMail/go-imap v0.0.0-20201228133358-4db68cea0cac h1:2xU3QncAiS/W3UlWZTkbNKW5WkLzk6Egl1T0xX+sbjs= github.com/ProtonMail/go-imap v0.0.0-20201228133358-4db68cea0cac/go.mod h1:yKASt+C3ZiDAiCSssxg9caIckWF/JG7ZQTO7GAmvicU= github.com/ProtonMail/go-message v0.0.0-20210611055058-fabeff2ec753 h1:I8IsYA297x0QLU80G5I6aLYUu3JYNSpo8j5fkXtFDW0= github.com/ProtonMail/go-message v0.0.0-20210611055058-fabeff2ec753/go.mod h1:NBAn21zgCJ/52WLDyed18YvYFm5tEoeDauubFqLokM4= -github.com/ProtonMail/go-mime v0.0.0-20190923161245-9b5a4261663a h1:W6RrgN/sTxg1msqzFFb+G80MFmpjMw61IU+slm+wln4= -github.com/ProtonMail/go-mime v0.0.0-20190923161245-9b5a4261663a/go.mod h1:NYt+V3/4rEeDuaev/zw1zCq8uqVEuPHzDPo3OZrlGJ4= +github.com/ProtonMail/go-mime v0.0.0-20220302105931-303f85f7fe0f h1:CGq7OieOz3wyQJ1fO8S0eO9TCW1JyvLrf8fhzz1i8ko= +github.com/ProtonMail/go-mime v0.0.0-20220302105931-303f85f7fe0f/go.mod h1:NYt+V3/4rEeDuaev/zw1zCq8uqVEuPHzDPo3OZrlGJ4= github.com/ProtonMail/go-rfc5322 v0.8.0 h1:7emrf75n3CDIduQflx7aT1nJa5h/kGsiFKUYX/+IAkU= github.com/ProtonMail/go-rfc5322 v0.8.0/go.mod h1:BwpTbkJxkMGkc+pC84AXZnwuWOisEULBpfPIyIKS/Us= -github.com/ProtonMail/go-srp v0.0.1 h1:J0O9Zb5XTC6iDrB7feH41cu+TUEB+l7uHctXIK6oS2o= -github.com/ProtonMail/go-srp v0.0.1/go.mod h1:Uvv5cqSGCs8MTZ8sbKiCkBnaB6/OA3eq2mc77tl2VVA= +github.com/ProtonMail/go-srp v0.0.5 h1:xhUioxZgDbCnpo9JehyFhwwsn9JLWkUGfB0oiKXgiGg= +github.com/ProtonMail/go-srp v0.0.5/go.mod h1:06iYHtLXW8vjLtccWj++x3MKy65sIT8yZd7nrJF49rs= github.com/ProtonMail/go-vcard v0.0.0-20180326232728-33aaa0a0c8a5 h1:Uga1DHFN4GUxuDQr0F71tpi8I9HqPIlZodZAI1lR6VQ= github.com/ProtonMail/go-vcard v0.0.0-20180326232728-33aaa0a0c8a5/go.mod h1:oeP9CMN+ajWp5jKp1kue5daJNwMMxLF+ujPaUIoJWlA= -github.com/ProtonMail/gopenpgp/v2 v2.4.1 h1:b3El0zabaKi73u4sRnb3hOOUczuKuYpN8wnp7wRsZSc= -github.com/ProtonMail/gopenpgp/v2 v2.4.1/go.mod h1:RFjoVjfhV8f78tjz/fLrp/OXkugL3QmWsiJq/fsQYA4= +github.com/ProtonMail/gopenpgp/v2 v2.4.7 h1:V3xeelvXgJiZXZuPtSSE+uYbtPw4RmbmyPqXDAESPhg= +github.com/ProtonMail/gopenpgp/v2 v2.4.7/go.mod h1:ZW1KxHNG6q5LMgFKf9Ap/d2eVYeyGf5+fAUEAjJWtmo= github.com/PuerkitoBio/goquery v1.5.1 h1:PSPBGne8NIUWw+/7vFBV+kG2J/5MOjbzc7154OaKCSE= github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo= github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/antlr/antlr4 v0.0.0-20201029161626-9a95f0cc3d7c h1:j/C2kxPfyE0d87/ggAjIsCV5Cdkqmjb+O0W8W+1J+IY= github.com/antlr/antlr4 v0.0.0-20201029161626-9a95f0cc3d7c/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y= -github.com/cronokirby/saferith v0.31.0 h1:TIlhldetKLeGAb19bZvWiuwQEzfzwSPthDEyJ9Ah8xs= -github.com/cronokirby/saferith v0.31.0/go.mod h1:QKJhjoqUtBsXCAVEjw38mFqoi7DebT7kthcD7UzbnoA= +github.com/cronokirby/saferith v0.33.0 h1:TgoQlfsD4LIwx71+ChfRcIpjkw+RPOapDEVxa+LhwLo= +github.com/cronokirby/saferith v0.33.0/go.mod h1:QKJhjoqUtBsXCAVEjw38mFqoi7DebT7kthcD7UzbnoA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -100,8 +102,8 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= -golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 h1:kUhD7nTDoI3fVd9G4ORWrbV5NY0liEs/Jg2pv5f+bBA= -golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d h1:sK3txAijHtOK88l68nt020reeT1ZdKLIYetKl95FzVY= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56/go.mod h1:JhuoJpWY28nO4Vef9tZUw9qufEGTyX1+7lmHxV5q5G4= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= @@ -135,8 +137,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220111092808-5a964db01320 h1:0jf+tOCoZ3LyutmCOWpVni1chK4VfFLhRsDK7MhqGRY= -golang.org/x/sys v0.0.0-20220111092808-5a964db01320/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b h1:2n253B2r0pYSmEV+UNCQoPfU/FiaizQEK5Gu4Bq4JE8= +golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/pkg/message/parser.go b/pkg/message/parser.go index 9376936..f0ef7ca 100644 --- a/pkg/message/parser.go +++ b/pkg/message/parser.go @@ -155,14 +155,6 @@ func convertForeignEncodings(p *parser.Parser) error { RegisterContentTypeHandler("text/.*", func(p *parser.Part) error { return p.ConvertToUTF8() }). - RegisterDefaultHandler(func(p *parser.Part) error { - // multipart/alternative, for example, can contain extra charset. - if _, params, _ := p.ContentType(); params != nil && params["charset"] != "" { - return p.ConvertToUTF8() - } - - return nil - }). Walk() } diff --git a/pkg/pmapi/client_types.go b/pkg/pmapi/client_types.go index c042096..48864e3 100644 --- a/pkg/pmapi/client_types.go +++ b/pkg/pmapi/client_types.go @@ -64,6 +64,13 @@ type Client interface { DeleteLabel(ctx context.Context, labelID string) error EmptyFolder(ctx context.Context, labelID string, addressID string) error + // /core/V4/labels routes + ListLabelsOnly(ctx context.Context) ([]*Label, error) + ListFoldersOnly(ctx context.Context) ([]*Label, error) + CreateLabelV4(ctx context.Context, label *Label) (*Label, error) + UpdateLabelV4(ctx context.Context, label *Label) (*Label, error) + DeleteLabelV4(ctx context.Context, labelID string) error + GetMailSettings(ctx context.Context) (MailSettings, error) GetContactEmailByEmail(context.Context, string, int, int) ([]ContactEmail, error) GetContactByID(context.Context, string) (Contact, error) diff --git a/pkg/pmapi/keyring.go b/pkg/pmapi/keyring.go index 1deead8..c3bc1bb 100644 --- a/pkg/pmapi/keyring.go +++ b/pkg/pmapi/keyring.go @@ -320,7 +320,7 @@ func encryptSymmDecryptKey( return } - pgpSplitMessage, err := pgpMessage.SeparateKeyAndData(len(textToEncrypt), 0) + pgpSplitMessage, err := pgpMessage.SplitMessage() if err != nil { return } diff --git a/pkg/pmapi/labels.go b/pkg/pmapi/labels.go index 3d1818d..97b6265 100644 --- a/pkg/pmapi/labels.go +++ b/pkg/pmapi/labels.go @@ -38,7 +38,7 @@ const ( DraftLabel = "8" StarredLabel = "10" - LabelTypeMailbox = 1 + LabelTypeMailBox = 1 LabelTypeContactGroup = 2 ) @@ -89,7 +89,7 @@ type Label struct { //nolint:maligned } func (c *client) ListLabels(ctx context.Context) (labels []*Label, err error) { - return c.listLabelType(ctx, LabelTypeMailbox) + return c.listLabelType(ctx, LabelTypeMailBox) } func (c *client) ListContactGroups(ctx context.Context) (labels []*Label, err error) { diff --git a/pkg/pmapi/labels_test.go b/pkg/pmapi/labels_test.go index d091795..8f82af8 100644 --- a/pkg/pmapi/labels_test.go +++ b/pkg/pmapi/labels_test.go @@ -53,8 +53,8 @@ const testLabelsBody = `{ ` var testLabels = []*Label{ - {ID: "LLz8ysmVxwr4dF6mWpClePT0SpSWOEvzTdq17RydSl4ndMckvY1K63HeXDzn03BJQwKYvgf-eWT8Qfd9WVuIEQ==", Name: "CroutonMail is awesome :)", Color: "#7272a7", Order: 1, Display: 0, Type: LabelTypeMailbox}, - {ID: "BvbqbySUPo9uWW_eR8tLA13NUsQMz3P4Zhw4UnpvrKqURnrHlE6L2Au0nplHfHlVXFgGz4L4hJ9-BYllOL-L5g==", Name: "Royal sausage", Color: "#cf5858", Order: 2, Display: 1, Type: LabelTypeMailbox}, + {ID: "LLz8ysmVxwr4dF6mWpClePT0SpSWOEvzTdq17RydSl4ndMckvY1K63HeXDzn03BJQwKYvgf-eWT8Qfd9WVuIEQ==", Name: "CroutonMail is awesome :)", Color: "#7272a7", Order: 1, Display: 0, Type: LabelTypeMailBox}, + {ID: "BvbqbySUPo9uWW_eR8tLA13NUsQMz3P4Zhw4UnpvrKqURnrHlE6L2Au0nplHfHlVXFgGz4L4hJ9-BYllOL-L5g==", Name: "Royal sausage", Color: "#cf5858", Order: 2, Display: 1, Type: LabelTypeMailBox}, } var testLabelReq = LabelReq{&Label{ @@ -82,7 +82,7 @@ var testLabelCreated = &Label{ Color: "#c26cc7", Order: 3, Display: 1, - Type: LabelTypeMailbox, + Type: LabelTypeMailBox, } const testDeleteLabelBody = `{ diff --git a/pkg/pmapi/labels_v4.go b/pkg/pmapi/labels_v4.go new file mode 100644 index 0000000..d337b83 --- /dev/null +++ b/pkg/pmapi/labels_v4.go @@ -0,0 +1,110 @@ +// Copyright (c) 2022 Proton AG +// +// This file is part of Proton Mail Bridge. +// +// Proton Mail Bridge is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Proton Mail Bridge is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Proton Mail Bridge. If not, see . + +package pmapi + +import ( + "context" + "errors" + "strconv" + + "github.com/go-resty/resty/v2" +) + +type LabelTypeV4 int + +const ( + LabelTypeV4Label = 1 + LabelTypeV4ContactGroup = 2 + LabelTypeV4Folder = 3 +) + +func (c *client) ListLabelsOnly(ctx context.Context) (labels []*Label, err error) { + return c.listLabelTypeV4(ctx, LabelTypeV4Label) +} + +func (c *client) ListFoldersOnly(ctx context.Context) (labels []*Label, err error) { + return c.listLabelTypeV4(ctx, LabelTypeV4Folder) +} + +// listLabelType lists all labels created by the user. +func (c *client) listLabelTypeV4(ctx context.Context, labelType LabelTypeV4) (labels []*Label, err error) { + var res struct { + Labels []*Label + } + + if _, err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { + return r.SetQueryParam("Type", strconv.Itoa(int(labelType))).SetResult(&res).Get("/core/v4/labels") + }); err != nil { + return nil, err + } + + return res.Labels, nil +} + +// CreateLabel creates a new label. +func (c *client) CreateLabelV4(ctx context.Context, label *Label) (created *Label, err error) { + if label.Name == "" { + return nil, errors.New("name is required") + } + + var res struct { + Label *Label + } + + if _, err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { + return r.SetBody(&LabelReq{ + Label: label, + }).SetResult(&res).Post("/core/v4/labels") + }); err != nil { + return nil, err + } + + return res.Label, nil +} + +// UpdateLabel updates a label. +func (c *client) UpdateLabelV4(ctx context.Context, label *Label) (updated *Label, err error) { + if label.Name == "" { + return nil, errors.New("name is required") + } + + var res struct { + Label *Label + } + + if _, err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { + return r.SetBody(&LabelReq{ + Label: label, + }).SetResult(&res).Put("/core/v4/labels/" + label.ID) + }); err != nil { + return nil, err + } + + return res.Label, nil +} + +// DeleteLabel deletes a label. +func (c *client) DeleteLabelV4(ctx context.Context, labelID string) error { + if _, err := c.do(ctx, func(r *resty.Request) (*resty.Response, error) { + return r.Delete("/core/v4/labels/" + labelID) + }); err != nil { + return err + } + + return nil +} diff --git a/pkg/pmapi/labels_v4_test.go b/pkg/pmapi/labels_v4_test.go new file mode 100644 index 0000000..8cbfcbc --- /dev/null +++ b/pkg/pmapi/labels_v4_test.go @@ -0,0 +1,170 @@ +// Copyright (c) 2022 Proton AG +// +// This file is part of Proton Mail Bridge. +// +// Proton Mail Bridge is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Proton Mail Bridge is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Proton Mail Bridge. If not, see . + +package pmapi + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "net/http" + "reflect" + "testing" + + r "github.com/stretchr/testify/require" +) + +const testFoldersBody = `{ + "Labels": [ + { + "ID": "LLz8ysmVxwr4dF6mWpClePT0SpSWOEvzTdq17RydSl4ndMckvY1K63HeXDzn03BJQwKYvgf-eWT8Qfd9WVuIEQ==", + "Name": "CroutonMail is awesome :)", + "Color": "#7272a7", + "Display": 0, + "Order": 1, + "Type": 3 + }, + { + "ID": "BvbqbySUPo9uWW_eR8tLA13NUsQMz3P4Zhw4UnpvrKqURnrHlE6L2Au0nplHfHlVXFgGz4L4hJ9-BYllOL-L5g==", + "Name": "Royal sausage", + "Color": "#cf5858", + "Display": 1, + "Order": 2, + "Type": 3 + } + ], + "Code": 1000 +} +` + +var testFolders = []*Label{ + {ID: "LLz8ysmVxwr4dF6mWpClePT0SpSWOEvzTdq17RydSl4ndMckvY1K63HeXDzn03BJQwKYvgf-eWT8Qfd9WVuIEQ==", Name: "CroutonMail is awesome :)", Color: "#7272a7", Order: 1, Display: 0, Type: LabelTypeV4Folder}, + {ID: "BvbqbySUPo9uWW_eR8tLA13NUsQMz3P4Zhw4UnpvrKqURnrHlE6L2Au0nplHfHlVXFgGz4L4hJ9-BYllOL-L5g==", Name: "Royal sausage", Color: "#cf5858", Order: 2, Display: 1, Type: LabelTypeV4Folder}, +} + +func TestClient_ListLabelsOnly(t *testing.T) { + s, c := newTestClient(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + r.NoError(t, checkMethodAndPath(req, "GET", "/core/v4/labels?Type=1")) + + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, testLabelsBody) + })) + defer s.Close() + + labels, err := c.ListLabelsOnly(context.Background()) + r.NoError(t, err) + r.Equal(t, testLabels, labels) +} + +func TestClient_ListFoldersOnly(t *testing.T) { + s, c := newTestClient(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + r.NoError(t, checkMethodAndPath(req, "GET", "/core/v4/labels?Type=3")) + + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, testFoldersBody) + })) + defer s.Close() + + folders, err := c.ListFoldersOnly(context.Background()) + r.NoError(t, err) + r.Equal(t, testFolders, folders) +} +func TestClient_CreateLabelV4(t *testing.T) { + s, c := newTestClient(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + r.NoError(t, checkMethodAndPath(req, "POST", "/core/v4/labels")) + + body := &bytes.Buffer{} + _, err := body.ReadFrom(req.Body) + r.NoError(t, err) + + if bytes.Contains(body.Bytes(), []byte("Order")) { + t.Fatal("Body contains `Order`: ", body.String()) + } + + var labelReq LabelReq + err = json.NewDecoder(body).Decode(&labelReq) + r.NoError(t, err) + r.Equal(t, testLabelReq.Label, labelReq.Label) + + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, testCreateLabelBody) + })) + defer s.Close() + + created, err := c.CreateLabelV4(context.Background(), testLabelReq.Label) + r.NoError(t, err) + + if !reflect.DeepEqual(created, testLabelCreated) { + t.Fatalf("Invalid created label: expected %+v, got %+v", testLabelCreated, created) + } +} + +func TestClient_CreateEmptyLabelV4(t *testing.T) { + s, c := newTestClient(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { + r.Fail(t, "API should not be called") + })) + defer s.Close() + + _, err := c.CreateLabelV4(context.Background(), &Label{}) + r.EqualError(t, err, "name is required") +} + +func TestClient_UpdateLabelV4(t *testing.T) { + s, c := newTestClient(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + r.NoError(t, checkMethodAndPath(req, "PUT", "/core/v4/labels/"+testLabelCreated.ID)) + + var labelReq LabelReq + err := json.NewDecoder(req.Body).Decode(&labelReq) + r.NoError(t, err) + r.Equal(t, testLabelCreated, labelReq.Label) + + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, testCreateLabelBody) + })) + defer s.Close() + + updated, err := c.UpdateLabelV4(context.Background(), testLabelCreated) + r.NoError(t, err) + + if !reflect.DeepEqual(updated, testLabelCreated) { + t.Fatalf("Invalid updated label: expected %+v, got %+v", testLabelCreated, updated) + } +} + +func TestClient_UpdateLabelToEmptyNameV4(t *testing.T) { + s, c := newTestClient(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) { + r.Fail(t, "API should not be called") + })) + defer s.Close() + + _, err := c.UpdateLabelV4(context.Background(), &Label{ID: "label"}) + r.EqualError(t, err, "name is required") +} + +func TestClient_DeleteLabelV4(t *testing.T) { + s, c := newTestClient(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + r.NoError(t, checkMethodAndPath(req, "DELETE", "/core/v4/labels/"+testLabelCreated.ID)) + + w.Header().Set("Content-Type", "application/json") + fmt.Fprint(w, testDeleteLabelBody) + })) + defer s.Close() + + err := c.DeleteLabelV4(context.Background(), testLabelCreated.ID) + r.NoError(t, err) +} diff --git a/pkg/pmapi/mocks/mocks.go b/pkg/pmapi/mocks/mocks.go index bdfbe94..55ef27e 100644 --- a/pkg/pmapi/mocks/mocks.go +++ b/pkg/pmapi/mocks/mocks.go @@ -169,6 +169,21 @@ func (mr *MockClientMockRecorder) CreateLabel(arg0, arg1 interface{}) *gomock.Ca return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateLabel", reflect.TypeOf((*MockClient)(nil).CreateLabel), arg0, arg1) } +// CreateLabelV4 mocks base method. +func (m *MockClient) CreateLabelV4(arg0 context.Context, arg1 *pmapi.Label) (*pmapi.Label, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CreateLabelV4", arg0, arg1) + ret0, _ := ret[0].(*pmapi.Label) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// CreateLabelV4 indicates an expected call of CreateLabelV4. +func (mr *MockClientMockRecorder) CreateLabelV4(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateLabelV4", reflect.TypeOf((*MockClient)(nil).CreateLabelV4), arg0, arg1) +} + // CurrentUser mocks base method. func (m *MockClient) CurrentUser(arg0 context.Context) (*pmapi.User, error) { m.ctrl.T.Helper() @@ -213,6 +228,21 @@ func (mr *MockClientMockRecorder) DeleteLabel(arg0, arg1 interface{}) *gomock.Ca return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteLabel", reflect.TypeOf((*MockClient)(nil).DeleteLabel), arg0, arg1) } +// DeleteLabelV4 mocks base method. +func (m *MockClient) DeleteLabelV4(arg0 context.Context, arg1 string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteLabelV4", arg0, arg1) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteLabelV4 indicates an expected call of DeleteLabelV4. +func (mr *MockClientMockRecorder) DeleteLabelV4(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteLabelV4", reflect.TypeOf((*MockClient)(nil).DeleteLabelV4), arg0, arg1) +} + + // DeleteMessages mocks base method. func (m *MockClient) DeleteMessages(arg0 context.Context, arg1 []string) error { m.ctrl.T.Helper() @@ -465,6 +495,36 @@ func (mr *MockClientMockRecorder) ListLabels(arg0 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLabels", reflect.TypeOf((*MockClient)(nil).ListLabels), arg0) } +// ListLabelsOnly mocks base method. +func (m *MockClient) ListLabelsOnly(arg0 context.Context) ([]*pmapi.Label, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListLabelsOnly", arg0) + ret0, _ := ret[0].([]*pmapi.Label) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListLabelsOnly indicates an expected call of ListLabelsOnly. +func (mr *MockClientMockRecorder) ListLabelsOnly(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListLabelsOnly", reflect.TypeOf((*MockClient)(nil).ListLabelsOnly), arg0) +} + +// ListFoldersOnly mocks base method. +func (m *MockClient) ListFoldersOnly(arg0 context.Context) ([]*pmapi.Label, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListFoldersOnly", arg0) + ret0, _ := ret[0].([]*pmapi.Label) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListFoldersOnly indicates an expected call of ListFoldersOnly. +func (mr *MockClientMockRecorder) ListFoldersOnly(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListFoldersOnly", reflect.TypeOf((*MockClient)(nil).ListFoldersOnly), arg0) +} + // ListMessages mocks base method. func (m *MockClient) ListMessages(arg0 context.Context, arg1 *pmapi.MessagesFilter) ([]*pmapi.Message, int, error) { m.ctrl.T.Helper() @@ -596,6 +656,21 @@ func (mr *MockClientMockRecorder) UpdateLabel(arg0, arg1 interface{}) *gomock.Ca return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateLabel", reflect.TypeOf((*MockClient)(nil).UpdateLabel), arg0, arg1) } +// UpdateLabelV4 mocks base method. +func (m *MockClient) UpdateLabelV4(arg0 context.Context, arg1 *pmapi.Label) (*pmapi.Label, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "UpdateLabelV4", arg0, arg1) + ret0, _ := ret[0].(*pmapi.Label) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// UpdateLabelV4 indicates an expected call of UpdateLabelV4. +func (mr *MockClientMockRecorder) UpdateLabelV4(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UpdateLabelUpdateLabelV4", reflect.TypeOf((*MockClient)(nil).UpdateLabelV4), arg0, arg1) +} + // UpdateUser mocks base method. func (m *MockClient) UpdateUser(arg0 context.Context) (*pmapi.User, error) { m.ctrl.T.Helper() diff --git a/pkg/store/mailbox_counts.go b/pkg/store/mailbox_counts.go index 797a82b..a7c8da7 100644 --- a/pkg/store/mailbox_counts.go +++ b/pkg/store/mailbox_counts.go @@ -128,7 +128,7 @@ func (mc *mailboxCounts) getPMLabel() *pmapi.Label { Path: mc.LabelName, Color: mc.Color, Order: mc.Order, - Type: pmapi.LabelTypeMailbox, + Type: pmapi.LabelTypeMailBox, Exclusive: pmapi.Boolean(mc.IsFolder), } } diff --git a/pkg/store/user_mailbox.go b/pkg/store/user_mailbox.go index 3ff55dd..8972307 100644 --- a/pkg/store/user_mailbox.go +++ b/pkg/store/user_mailbox.go @@ -59,7 +59,7 @@ func (store *Store) createMailbox(name string) error { Name: name, Color: color, Exclusive: pmapi.Boolean(exclusive), - Type: pmapi.LabelTypeMailbox, + Type: pmapi.LabelTypeMailBox, }) return err } @@ -188,7 +188,7 @@ func (store *Store) createOrUpdateMailboxEvent(label *pmapi.Label) error { store.lock.Lock() defer store.lock.Unlock() - if label.Type != pmapi.LabelTypeMailbox { + if label.Type != pmapi.LabelTypeMailBox { return nil }