proton-bridge/tests/ctx_bridge_test.go

129 lines
2.9 KiB
Go
Raw Normal View History

// 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 <https://www.gnu.org/licenses/>.
2022-08-26 15:00:21 +00:00
package tests
import (
"context"
"crypto/tls"
2022-08-26 15:00:21 +00:00
"fmt"
2022-10-11 17:40:28 +00:00
"net/http/cookiejar"
"os"
"time"
2022-08-26 15:00:21 +00:00
"github.com/sirupsen/logrus"
2022-08-26 15:00:21 +00:00
"github.com/ProtonMail/proton-bridge/v2/internal/bridge"
2022-10-11 17:40:28 +00:00
"github.com/ProtonMail/proton-bridge/v2/internal/cookies"
2022-08-26 15:00:21 +00:00
"github.com/ProtonMail/proton-bridge/v2/internal/events"
"github.com/ProtonMail/proton-bridge/v2/internal/useragent"
"github.com/ProtonMail/proton-bridge/v2/internal/vault"
"gitlab.protontech.ch/go/liteapi"
2022-08-26 15:00:21 +00:00
)
func (t *testCtx) startBridge() error {
// Bridge will enable the proxy by default at startup.
t.mocks.ProxyCtl.EXPECT().AllowProxy()
2022-08-26 15:00:21 +00:00
// Get the path to the vault.
vaultDir, err := t.locator.ProvideSettingsPath()
if err != nil {
return err
}
// Get the default gluon path.
gluonDir, err := t.locator.ProvideGluonPath()
if err != nil {
return err
}
// Create the vault.
vault, corrupt, err := vault.New(vaultDir, gluonDir, t.storeKey)
if err != nil {
return err
} else if corrupt {
return fmt.Errorf("vault is corrupt")
}
2022-10-11 21:24:20 +00:00
// Create the underlying cookie jar.
2022-10-11 17:40:28 +00:00
jar, err := cookiejar.New(nil)
if err != nil {
return err
}
2022-10-11 21:24:20 +00:00
// Create the persisting cookie jar.
2022-10-11 17:40:28 +00:00
persister, err := cookies.NewCookieJar(jar, vault)
if err != nil {
return err
}
var logIMAP bool
if len(os.Getenv("FEATURE_TEST_LOG_IMAP")) != 0 {
logrus.SetLevel(logrus.TraceLevel)
logIMAP = true
}
2022-08-26 15:00:21 +00:00
// Create the bridge.
2022-10-11 22:20:04 +00:00
bridge, eventCh, err := bridge.New(
// App stuff
2022-08-26 15:00:21 +00:00
t.locator,
vault,
2022-10-11 17:40:28 +00:00
t.mocks.Autostarter,
t.mocks.Updater,
t.version,
// API stuff
2022-10-11 17:40:28 +00:00
t.api.GetHostURL(),
persister,
2022-08-26 15:00:21 +00:00
useragent.New(),
t.mocks.TLSReporter,
liteapi.NewDialer(t.netCtl, &tls.Config{InsecureSkipVerify: true}).GetRoundTripper(),
t.mocks.ProxyCtl,
2022-10-11 17:40:28 +00:00
// Logging stuff
logIMAP,
logIMAP,
false,
2022-08-26 15:00:21 +00:00
)
if err != nil {
return err
}
t.events.collectFrom(eventCh)
2022-10-11 22:20:04 +00:00
// Wait for the users to be loaded.
t.events.await(events.AllUsersLoaded{}, 30*time.Second)
2022-10-11 22:20:04 +00:00
// Save the bridge to the context.
2022-08-26 15:00:21 +00:00
t.bridge = bridge
return nil
}
func (t *testCtx) stopBridge() error {
if t.bridge == nil {
return fmt.Errorf("bridge is not running")
2022-08-26 15:00:21 +00:00
}
t.bridge.Close(context.Background())
2022-08-26 15:00:21 +00:00
t.bridge = nil
return nil
}