mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-17 23:56:56 +00:00
GODT-1166: Reduce the number of auth for live test
- Changed: Do not reauth controller clients. - Changed: Verbosisty is set only once before run - Changed: AddUser takes TestAccount as argument - Added: Setup/clean up before/after test run - Added: Access to the current refresh token from pmapi.Client interface. - Added: Context function to add test a user to bridge without login, just call users.FinishLogin. - Added: PMAPIController.GetAuthClient returns authenticated client for username. - Added: Persistent clients does not loggout after every scenario. - Changed: Disabled no-internet tests.
This commit is contained in:
@ -63,3 +63,13 @@ func (api *FakePMAPI) AuthDelete(_ context.Context) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (api *FakePMAPI) GetCurrentAuth() *pmapi.Auth {
|
||||
return &pmapi.Auth{
|
||||
UserID: api.userID,
|
||||
AuthRefresh: pmapi.AuthRefresh{
|
||||
UID: api.uid,
|
||||
RefreshToken: api.ref,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,6 +24,8 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// Controller implements dummy PMAPIController interface without actual
|
||||
// endpoint.
|
||||
type Controller struct {
|
||||
// Internal states.
|
||||
lock *sync.RWMutex
|
||||
|
||||
@ -22,8 +22,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
|
||||
"github.com/ProtonMail/proton-bridge/test/accounts"
|
||||
)
|
||||
|
||||
var systemLabelNameToID = map[string]string{ //nolint[gochecknoglobals]
|
||||
@ -61,13 +63,15 @@ func (ctl *Controller) ReorderAddresses(user *pmapi.User, addressIDs []string) e
|
||||
return api.ReorderAddresses(context.Background(), addressIDs)
|
||||
}
|
||||
|
||||
func (ctl *Controller) AddUser(user *pmapi.User, addresses *pmapi.AddressList, password []byte, twoFAEnabled bool) error {
|
||||
ctl.usersByUsername[user.Name] = &fakeUser{
|
||||
user: user,
|
||||
password: password,
|
||||
has2FA: twoFAEnabled,
|
||||
func (ctl *Controller) AddUser(account *accounts.TestAccount) error {
|
||||
ctl.usersByUsername[account.User().Name] = &fakeUser{
|
||||
user: account.User(),
|
||||
password: account.Password(),
|
||||
has2FA: account.IsTwoFAEnabled(),
|
||||
}
|
||||
ctl.addressesByUsername[user.Name] = addresses
|
||||
ctl.addressesByUsername[account.User().Name] = account.Addresses()
|
||||
ctl.createSession(account.User().Name, true)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -181,3 +185,15 @@ func (ctl *Controller) GetMessages(username, labelID string) ([]*pmapi.Message,
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
func (ctl *Controller) GetAuthClient(username string) pmapi.Client {
|
||||
for uid, session := range ctl.sessionsByUID {
|
||||
if session.username == username {
|
||||
return ctl.clientManager.NewClient(uid, session.acc, session.ref, time.Now())
|
||||
}
|
||||
}
|
||||
|
||||
ctl.log.WithField("username", username).Fatal("Cannot get authenticated client.")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -51,24 +51,25 @@ func (ctl *Controller) checkScope(uid string) bool {
|
||||
}
|
||||
|
||||
func (ctl *Controller) createSessionIfAuthorized(username string, password []byte) (*fakeSession, error) {
|
||||
// get user
|
||||
user, ok := ctl.usersByUsername[username]
|
||||
if !ok || !bytes.Equal(user.password, password) {
|
||||
return nil, errWrongNameOrPassword
|
||||
}
|
||||
|
||||
// create session
|
||||
return ctl.createSession(username, !user.has2FA), nil
|
||||
}
|
||||
|
||||
func (ctl *Controller) createSession(username string, hasFullScope bool) *fakeSession {
|
||||
session := &fakeSession{
|
||||
username: username,
|
||||
uid: ctl.tokenGenerator.next("uid"),
|
||||
acc: ctl.tokenGenerator.next("acc"),
|
||||
ref: ctl.tokenGenerator.next("ref"),
|
||||
hasFullScope: !user.has2FA,
|
||||
hasFullScope: hasFullScope,
|
||||
}
|
||||
|
||||
ctl.sessionsByUID[session.uid] = session
|
||||
|
||||
return session, nil
|
||||
return session
|
||||
}
|
||||
|
||||
func (ctl *Controller) refreshSessionIfAuthorized(uid, ref string) (*fakeSession, error) {
|
||||
|
||||
Reference in New Issue
Block a user