GODT-35: Finish all details and make tests pass

This commit is contained in:
Michal Horejsek
2021-03-11 14:37:15 +01:00
committed by Jakub
parent 2284e9ede1
commit 8109831c07
173 changed files with 4697 additions and 2897 deletions

View File

@ -23,8 +23,8 @@ import (
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
)
func (api *FakePMAPI) Auth2FA(_ context.Context, req pmapi.Auth2FAReq) error {
if err := api.checkAndRecordCall(POST, "/auth/2fa", req); err != nil {
func (api *FakePMAPI) Auth2FA(_ context.Context, twoFactorCode string) error {
if err := api.checkAndRecordCall(POST, "/auth/2fa", twoFactorCode); err != nil {
return err
}
@ -50,7 +50,7 @@ func (api *FakePMAPI) AuthSalt(_ context.Context) (string, error) {
return "", nil
}
func (api *FakePMAPI) AddAuthHandler(handler pmapi.AuthHandler) {
func (api *FakePMAPI) AddAuthRefreshHandler(handler pmapi.AuthRefreshHandler) {
api.authHandlers = append(api.authHandlers, handler)
}

View File

@ -32,7 +32,7 @@ type Controller struct {
labelIDGenerator idGenerator
messageIDGenerator idGenerator
tokenGenerator idGenerator
clientManager pmapi.Manager
clientManager *fakePMAPIManager
// State controlled by test.
noInternetConnection bool

View File

@ -40,7 +40,7 @@ type fakeCall struct {
request []byte
}
func (ctl *Controller) recordCall(method method, path string, req interface{}) error {
func (ctl *Controller) checkAndRecordCall(method method, path string, req interface{}) error {
ctl.lock.Lock()
defer ctl.lock.Unlock()
@ -50,7 +50,7 @@ func (ctl *Controller) recordCall(method method, path string, req interface{}) e
var err error
if request, err = json.Marshal(req); err != nil {
return err
panic(err)
}
}

View File

@ -39,11 +39,17 @@ var systemLabelNameToID = map[string]string{ //nolint[gochecknoglobals]
func (ctl *Controller) TurnInternetConnectionOff() {
ctl.log.Warn("Turning OFF internet")
ctl.noInternetConnection = true
for _, observer := range ctl.clientManager.connectionObservers {
observer.OnDown()
}
}
func (ctl *Controller) TurnInternetConnectionOn() {
ctl.log.Warn("Turning ON internet")
ctl.noInternetConnection = false
for _, observer := range ctl.clientManager.connectionObservers {
observer.OnUp()
}
}
func (ctl *Controller) ReorderAddresses(user *pmapi.User, addressIDs []string) error {
@ -52,7 +58,7 @@ func (ctl *Controller) ReorderAddresses(user *pmapi.User, addressIDs []string) e
return errors.New("no such user")
}
return api.ReorderAddresses(context.TODO(), addressIDs)
return api.ReorderAddresses(context.Background(), addressIDs)
}
func (ctl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList, password string, twoFAEnabled bool) error {
@ -79,7 +85,7 @@ func (ctl *Controller) AddUserLabel(username string, label *pmapi.Label) error {
label.Exclusive = getLabelExclusive(label.Name)
prefix := "label"
if label.Exclusive == 1 {
if label.Exclusive {
prefix = "folder"
}
label.ID = ctl.labelIDGenerator.next(prefix)
@ -127,11 +133,8 @@ 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/"))
}
func (ctl *Controller) AddUserMessage(username string, message *pmapi.Message) (string, error) {

View File

@ -43,13 +43,12 @@ func (api *FakePMAPI) getCounts(addressID string) []*pmapi.MessagesCount {
for _, labelID := range message.LabelIDs {
if counts, ok := allCounts[labelID]; ok {
counts.Total++
if message.Unread == 1 {
if message.Unread {
counts.Unread++
}
} else {
var unread int
if message.Unread == pmapi.True {
if message.Unread {
unread = 1
}

View File

@ -34,7 +34,7 @@ type FakePMAPI struct {
controller *Controller
eventIDGenerator idGenerator
authHandlers []pmapi.AuthHandler
authHandlers []pmapi.AuthRefreshHandler
user *pmapi.User
userKeyRing *crypto.KeyRing
addresses *pmapi.AddressList
@ -45,25 +45,13 @@ type FakePMAPI struct {
// uid represents the API UID. It is the unique session ID.
uid string
acc string // FIXME(conman): Check this is correct!
ref string // FIXME(conman): Check this is correct!
acc string
ref string
log *logrus.Entry
}
func newFakePMAPI(controller *Controller, userID, uid, acc, ref string) *FakePMAPI {
return &FakePMAPI{
controller: controller,
log: logrus.WithField("pkg", "fakeapi").WithField("uid", uid),
uid: uid,
acc: acc, // FIXME(conman): This should be checked!
ref: ref, // FIXME(conman): This should be checked!
userID: userID,
addrKeyRing: make(map[string]*crypto.KeyRing),
}
}
func NewFakePMAPI(controller *Controller, username, userID, uid, acc, ref string) (*FakePMAPI, error) {
func newFakePMAPI(controller *Controller, username, userID, uid, acc, ref string) (*FakePMAPI, error) {
user, ok := controller.usersByUsername[username]
if !ok {
return nil, fmt.Errorf("user %s does not exist", username)
@ -84,19 +72,28 @@ func NewFakePMAPI(controller *Controller, username, userID, uid, acc, ref string
messages = []*pmapi.Message{}
}
fakePMAPI := newFakePMAPI(controller, userID, uid, acc, ref)
fakePMAPI := &FakePMAPI{
username: username,
userID: userID,
controller: controller,
fakePMAPI.log = fakePMAPI.log.WithField("username", username)
fakePMAPI.username = username
fakePMAPI.user = user.user
fakePMAPI.addresses = addresses
fakePMAPI.labels = labels
fakePMAPI.messages = messages
user: user.user,
addresses: addresses,
labels: labels,
messages: messages,
uid: uid,
acc: acc,
ref: ref,
addrKeyRing: make(map[string]*crypto.KeyRing),
log: logrus.WithField("pkg", "fakeapi").WithField("uid", uid).WithField("username", username),
}
fakePMAPI.addEvent(&pmapi.Event{
EventID: fakePMAPI.eventIDGenerator.last("event"),
Refresh: 0,
More: 0,
More: false,
})
return fakePMAPI, nil
@ -112,13 +109,14 @@ func (api *FakePMAPI) checkAndRecordCall(method method, path string, request int
api.log.WithField(string(method), path).Trace("CALL")
if err := api.controller.recordCall(method, path, request); err != nil {
if err := api.controller.checkAndRecordCall(method, path, request); err != nil {
return err
}
// FIXME(conman): This needs to match conman behaviour. Should try auth refresh somehow.
if !api.controller.checkAccessToken(api.uid, api.acc) {
return pmapi.ErrUnauthorized
if err := api.authRefresh(); err != nil {
return err
}
}
if path != "/auth/2fa" && !api.controller.checkScope(api.uid) {
@ -128,6 +126,21 @@ func (api *FakePMAPI) checkAndRecordCall(method method, path string, request int
return nil
}
func (api *FakePMAPI) authRefresh() error {
if err := api.controller.checkAndRecordCall(POST, "/auth/refresh", []string{api.uid, api.ref}); err != nil {
return err
}
session, err := api.controller.refreshSessionIfAuthorized(api.uid, api.ref)
if err != nil {
return err
}
api.ref = session.ref
api.acc = session.acc
return nil
}
func (api *FakePMAPI) setUser(username string) error {
api.username = username
api.log = api.log.WithField("username", username)
@ -158,12 +171,3 @@ func (api *FakePMAPI) setUser(username string) error {
return nil
}
func (api *FakePMAPI) unsetUser() {
api.uid = ""
api.acc = "" // FIXME(conman): This should be checked!
api.user = nil
api.labels = nil
api.messages = nil
api.events = nil
}

View File

@ -27,7 +27,7 @@ import (
func (api *FakePMAPI) isLabelFolder(labelID string) bool {
for _, label := range api.labels {
if label.ID == labelID {
return label.Exclusive == 1
return bool(label.Exclusive)
}
}
return labelID == pmapi.InboxLabel || labelID == pmapi.ArchiveLabel || labelID == pmapi.SentLabel
@ -50,7 +50,7 @@ func (api *FakePMAPI) CreateLabel(_ context.Context, label *pmapi.Label) (*pmapi
}
}
prefix := "label"
if label.Exclusive == 1 {
if label.Exclusive {
prefix = "folder"
}
label.ID = api.controller.labelIDGenerator.next(prefix)

View File

@ -1,3 +1,20 @@
// Copyright (c) 2021 Proton Technologies AG
//
// This file is part of ProtonMail Bridge.
//
// ProtonMail 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.
//
// ProtonMail 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 ProtonMail Bridge. If not, see <https://www.gnu.org/licenses/>.
package fakeapi
import (
@ -8,27 +25,36 @@ import (
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
"github.com/go-resty/resty/v2"
"github.com/sirupsen/logrus"
)
type fakePMAPIManager struct {
controller *Controller
controller *Controller
connectionObservers []pmapi.ConnectionObserver
}
func (m *fakePMAPIManager) NewClient(uid string, acc string, ref string, _ time.Time) pmapi.Client {
if uid == "" {
return &FakePMAPI{
controller: m.controller,
log: logrus.WithField("pkg", "fakeapi"),
addrKeyRing: make(map[string]*crypto.KeyRing),
}
}
session, ok := m.controller.sessionsByUID[uid]
if !ok {
return newFakePMAPI(m.controller, "", "", "", "")
panic("session " + uid + " is missing")
}
user, ok := m.controller.usersByUsername[session.username]
if !ok {
return newFakePMAPI(m.controller, "", "", "", "")
panic("user " + session.username + " from session " + uid + " is missing")
}
client, err := NewFakePMAPI(m.controller, session.username, user.user.ID, session.uid, session.acc, session.ref)
client, err := newFakePMAPI(m.controller, session.username, user.user.ID, session.uid, session.acc, session.ref)
if err != nil {
return newFakePMAPI(m.controller, "", "", "", "")
panic(err)
}
m.controller.fakeAPIs = append(m.controller.fakeAPIs, client)
@ -36,15 +62,8 @@ func (m *fakePMAPIManager) NewClient(uid string, acc string, ref string, _ time.
return client
}
func (m *fakePMAPIManager) NewClientWithRefresh(_ context.Context, uid, ref string) (pmapi.Client, *pmapi.Auth, error) {
if err := m.controller.recordCall(POST, "/auth/refresh", &pmapi.AuthRefreshReq{
UID: uid,
RefreshToken: ref,
ResponseType: "token",
GrantType: "refresh_token",
RedirectURI: "https://protonmail.ch",
State: "random_string",
}); err != nil {
func (m *fakePMAPIManager) NewClientWithRefresh(_ context.Context, uid, ref string) (pmapi.Client, *pmapi.AuthRefresh, error) {
if err := m.controller.checkAndRecordCall(POST, "/auth/refresh", []string{uid, ref}); err != nil {
return nil, nil, err
}
@ -58,31 +77,25 @@ func (m *fakePMAPIManager) NewClientWithRefresh(_ context.Context, uid, ref stri
return nil, nil, errWrongNameOrPassword
}
client, err := NewFakePMAPI(m.controller, session.username, user.user.ID, session.uid, session.acc, session.ref)
client, err := newFakePMAPI(m.controller, session.username, user.user.ID, session.uid, session.acc, session.ref)
if err != nil {
return nil, nil, err
}
m.controller.fakeAPIs = append(m.controller.fakeAPIs, client)
auth := &pmapi.Auth{
auth := &pmapi.AuthRefresh{
UID: session.uid,
AccessToken: session.acc,
RefreshToken: session.ref,
ExpiresIn: 86400, // seconds,
}
if user.has2FA {
auth.TwoFA = pmapi.TwoFAInfo{
Enabled: pmapi.TOTPEnabled,
}
}
return client, auth, nil
}
func (m *fakePMAPIManager) NewClientWithLogin(_ context.Context, username string, password string) (pmapi.Client, *pmapi.Auth, error) {
if err := m.controller.recordCall(POST, "/auth/info", &pmapi.GetAuthInfoReq{Username: username}); err != nil {
if err := m.controller.checkAndRecordCall(POST, "/auth/info", &pmapi.GetAuthInfoReq{Username: username}); err != nil {
return nil, nil, err
}
@ -93,7 +106,7 @@ func (m *fakePMAPIManager) NewClientWithLogin(_ context.Context, username string
return nil, nil, errWrongNameOrPassword
}
if err := m.controller.recordCall(POST, "/auth", &pmapi.AuthReq{Username: username}); err != nil {
if err := m.controller.checkAndRecordCall(POST, "/auth", &pmapi.AuthReq{Username: username}); err != nil {
return nil, nil, err
}
@ -102,7 +115,7 @@ func (m *fakePMAPIManager) NewClientWithLogin(_ context.Context, username string
return nil, nil, err
}
client, err := NewFakePMAPI(m.controller, username, user.user.ID, session.uid, session.acc, session.ref)
client, err := newFakePMAPI(m.controller, username, user.user.ID, session.uid, session.acc, session.ref)
if err != nil {
return nil, nil, err
}
@ -110,14 +123,17 @@ func (m *fakePMAPIManager) NewClientWithLogin(_ context.Context, username string
m.controller.fakeAPIs = append(m.controller.fakeAPIs, client)
auth := &pmapi.Auth{
UID: session.uid,
AccessToken: session.acc,
RefreshToken: session.ref,
ExpiresIn: 86400, // seconds,
UserID: user.user.ID,
AuthRefresh: pmapi.AuthRefresh{
UID: session.uid,
AccessToken: session.acc,
RefreshToken: session.ref,
ExpiresIn: 86400, // seconds,
},
}
if user.has2FA {
auth.TwoFA = pmapi.TwoFAInfo{
auth.TwoFA = &pmapi.TwoFAInfo{
Enabled: pmapi.TOTPEnabled,
}
}
@ -125,40 +141,46 @@ func (m *fakePMAPIManager) NewClientWithLogin(_ context.Context, username string
return client, auth, nil
}
func (*fakePMAPIManager) DownloadAndVerify(kr *crypto.KeyRing, url, sig string) ([]byte, error) {
panic("TODO")
func (m *fakePMAPIManager) DownloadAndVerify(kr *crypto.KeyRing, url, sig string) ([]byte, error) {
panic("Not implemented: not used by tests")
}
func (*fakePMAPIManager) ReportBug(context.Context, pmapi.ReportBugReq) error {
panic("TODO")
func (m *fakePMAPIManager) ReportBug(_ context.Context, bugReport pmapi.ReportBugReq) error {
return m.controller.checkAndRecordCall(POST, "/reports/bug", bugReport)
}
func (m *fakePMAPIManager) SendSimpleMetric(_ context.Context, cat string, act string, lab string) error {
v := url.Values{}
v.Set("Category", cat)
v.Set("Action", act)
v.Set("Label", lab)
return m.controller.recordCall(GET, "/metrics?"+v.Encode(), nil)
return m.controller.checkAndRecordCall(GET, "/metrics?"+v.Encode(), nil)
}
func (*fakePMAPIManager) SetLogger(resty.Logger) {
panic("TODO")
func (m *fakePMAPIManager) SetLogging(*logrus.Entry, bool) {
// NOOP
}
func (*fakePMAPIManager) SetTransport(http.RoundTripper) {
panic("TODO")
func (m *fakePMAPIManager) SetTransport(http.RoundTripper) {
// NOOP
}
func (*fakePMAPIManager) SetCookieJar(http.CookieJar) {
panic("TODO")
func (m *fakePMAPIManager) SetCookieJar(http.CookieJar) {
// NOOP
}
func (*fakePMAPIManager) SetRetryCount(int) {
panic("TODO")
func (m *fakePMAPIManager) SetRetryCount(int) {
// NOOP
}
func (*fakePMAPIManager) AddConnectionObserver(pmapi.ConnectionObserver) {
panic("TODO")
func (m *fakePMAPIManager) AddConnectionObserver(connectionObserver pmapi.ConnectionObserver) {
m.connectionObservers = append(m.connectionObservers, connectionObserver)
}
func (m *fakePMAPIManager) AllowProxy() {
// NOOP
}
func (m *fakePMAPIManager) DisallowProxy() {
// NOOP
}

View File

@ -132,15 +132,7 @@ func isMessageMatchingFilter(filter *pmapi.MessagesFilter, message *pmapi.Messag
return false
}
if filter.Unread != nil {
var wantUnread pmapi.Boolean
if *filter.Unread {
wantUnread = pmapi.True
} else {
wantUnread = pmapi.False
}
if message.Unread != wantUnread {
if bool(message.Unread) != *filter.Unread {
return false
}
}
@ -393,10 +385,10 @@ func (api *FakePMAPI) MarkMessagesRead(_ context.Context, apiIDs []string) error
return api.updateMessages(PUT, "/mail/v4/messages/read", &pmapi.MessagesActionReq{
IDs: apiIDs,
}, apiIDs, func(message *pmapi.Message) error {
if message.Unread == 0 {
if !message.Unread {
return errWasNotUpdated
}
message.Unread = 0
message.Unread = false
return nil
})
}
@ -405,10 +397,10 @@ func (api *FakePMAPI) MarkMessagesUnread(_ context.Context, apiIDs []string) err
err := api.updateMessages(PUT, "/mail/v4/messages/unread", &pmapi.MessagesActionReq{
IDs: apiIDs,
}, apiIDs, func(message *pmapi.Message) error {
if message.Unread == 1 {
if message.Unread {
return errWasNotUpdated
}
message.Unread = 1
message.Unread = true
return nil
})
if err != nil {

View File

@ -116,6 +116,12 @@ func (api *FakePMAPI) ReorderAddresses(_ context.Context, addressIDs []string) e
}
func (api *FakePMAPI) Addresses() pmapi.AddressList {
if api.controller.noInternetConnection {
return nil
}
if api.addresses == nil {
return pmapi.AddressList{}
}
return *api.addresses
}