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

@ -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
}