Skip to content

Commit

Permalink
Strip core.CombinedAPI
Browse files Browse the repository at this point in the history
In some cases abstract CombinedAPI were used when some specific
functions were needed and they were already available in RPC
  • Loading branch information
Savolro committed Oct 9, 2024
1 parent 158157f commit 559b83b
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 84 deletions.
4 changes: 1 addition & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type CredentialsAPI interface {
DeleteToken(string) error
TrustedPassToken(string) (*TrustedPassTokenResponse, error)
MultifactorAuthStatus(string) (*MultifactorAuthStatusResponse, error)
Logout(token string) error
}

type InsightsAPI interface {
Expand All @@ -46,12 +47,9 @@ type ServersAPI interface {
}

type CombinedAPI interface {
CredentialsAPI
InsightsAPI
ServersAPI
Base() string
Plans() (*Plans, error)
Logout(token string) error
CreateUser(email, password string) (*UserCreateResponse, error)
}

Expand Down
2 changes: 1 addition & 1 deletion daemon/job_server_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
// JobServerCheck marks servers as offline if connection to them drops
func JobServerCheck(
dm *DataManager,
api core.CombinedAPI,
api core.ServersAPI,
netw networker.Networker,
server core.Server,
) func() {
Expand Down
6 changes: 3 additions & 3 deletions daemon/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (r *RPC) StartJobs(statePublisher *state.StatePublisher) {
// order of the jobs below matters
// servers job requires geo info and configs data to create server list
// TODO what if configs file is deleted just before servers job or disk is full?
if _, err := r.scheduler.NewJob(gocron.DurationJob(6*time.Hour), gocron.NewTask(JobCountries(r.dm, r.api)), gocron.WithName("job countries")); err != nil {
if _, err := r.scheduler.NewJob(gocron.DurationJob(6*time.Hour), gocron.NewTask(JobCountries(r.dm, r.serversAPI)), gocron.WithName("job countries")); err != nil {
log.Println(internal.WarningPrefix, "job countries schedule error:", err)
}

Expand All @@ -34,12 +34,12 @@ func (r *RPC) StartJobs(statePublisher *state.StatePublisher) {
log.Println(internal.WarningPrefix, "job insights schedule error:", err)
}

if _, err := r.scheduler.NewJob(gocron.DurationJob(1*time.Hour), gocron.NewTask(JobServers(r.dm, r.cm, r.api, true)), gocron.WithName("job servers")); err != nil {
if _, err := r.scheduler.NewJob(gocron.DurationJob(1*time.Hour), gocron.NewTask(JobServers(r.dm, r.cm, r.serversAPI, true)), gocron.WithName("job servers")); err != nil {
log.Println(internal.WarningPrefix, "job servers schedule error:", err)
}
// TODO if autoconnect runs before servers job, it will return zero servers list

if _, err := r.scheduler.NewJob(gocron.DurationJob(15*time.Minute), gocron.NewTask(JobServerCheck(r.dm, r.api, r.netw, r.lastServer)), gocron.WithName("job servers check")); err != nil {
if _, err := r.scheduler.NewJob(gocron.DurationJob(15*time.Minute), gocron.NewTask(JobServerCheck(r.dm, r.serversAPI, r.netw, r.lastServer)), gocron.WithName("job servers check")); err != nil {
log.Println(internal.WarningPrefix, "job servers check schedule error:", err)
}

Expand Down
5 changes: 3 additions & 2 deletions daemon/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/NordSecurity/nordvpn-linux/sharedctx"
"github.com/NordSecurity/nordvpn-linux/test/category"
"github.com/NordSecurity/nordvpn-linux/test/mock"
testcore "github.com/NordSecurity/nordvpn-linux/test/mock/core"
testfirewall "github.com/NordSecurity/nordvpn-linux/test/mock/firewall"
testnetworker "github.com/NordSecurity/nordvpn-linux/test/mock/networker"
testnorduser "github.com/NordSecurity/nordvpn-linux/test/mock/norduser/service"
Expand Down Expand Up @@ -103,7 +104,7 @@ func TestStartAutoConnect(t *testing.T) {
dm,
api,
test.serversAPI,
&validCredentialsAPI{},
&testcore.CredentialsAPIMock{},
testNewCDNAPI(),
testNewRepoAPI(),
&mockAuthenticationAPI{},
Expand Down Expand Up @@ -270,7 +271,7 @@ func TestStartAutoMeshnet(t *testing.T) {
testNewDataManager(),
api,
test.serversAPI,
&validCredentialsAPI{},
&testcore.CredentialsAPIMock{},
testNewCDNAPI(),
testNewRepoAPI(),
&mockAuthenticationAPI{},
Expand Down
66 changes: 3 additions & 63 deletions daemon/rpc_connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/NordSecurity/nordvpn-linux/sharedctx"
"github.com/NordSecurity/nordvpn-linux/test/category"
"github.com/NordSecurity/nordvpn-linux/test/mock"
testcore "github.com/NordSecurity/nordvpn-linux/test/mock/core"
testfirewall "github.com/NordSecurity/nordvpn-linux/test/mock/firewall"
testnetworker "github.com/NordSecurity/nordvpn-linux/test/mock/networker"
testnorduser "github.com/NordSecurity/nordvpn-linux/test/mock/norduser/service"
Expand All @@ -45,57 +46,6 @@ func (mockAuthenticationAPI) Token(string) (*core.LoginResponse, error) {
return nil, nil
}

type validCredentialsAPI struct {
renewToken string
}

func (validCredentialsAPI) NotificationCredentials(token, appUserID string) (core.NotificationCredentialsResponse, error) {
return core.NotificationCredentialsResponse{}, nil
}

func (v validCredentialsAPI) NotificationCredentialsRevoke(token, appUserID string, purgeSession bool) (core.NotificationCredentialsRevokeResponse, error) {
return core.NotificationCredentialsRevokeResponse{}, nil
}

func (validCredentialsAPI) ServiceCredentials(string) (*core.CredentialsResponse, error) {
return &core.CredentialsResponse{
NordlynxPrivateKey: "nordpriv",
Username: "elite",
Password: "hacker",
}, nil
}

func (v validCredentialsAPI) TokenRenew(renewToken string) (*core.TokenRenewResponse, error) {
return &core.TokenRenewResponse{
RenewToken: v.renewToken,
}, nil
}

func (validCredentialsAPI) DeleteToken(token string) error {
return nil
}

func (validCredentialsAPI) TrustedPassToken(token string) (*core.TrustedPassTokenResponse, error) {
return nil, nil
}

func (validCredentialsAPI) MultifactorAuthStatus(token string) (*core.MultifactorAuthStatusResponse, error) {
return nil, nil
}

func (validCredentialsAPI) Services(string) (core.ServicesResponse, error) {
return core.ServicesResponse{
{
ExpiresAt: "2029-12-27 00:00:00",
Service: core.Service{ID: 1},
},
}, nil
}

func (validCredentialsAPI) CurrentUser(string) (*core.CurrentUserResponse, error) {
return nil, nil
}

type workingLoginChecker struct {
isVPNExpired bool
vpnErr error
Expand Down Expand Up @@ -124,16 +74,6 @@ type mockAnalytics struct{}
func (*mockAnalytics) Enable() error { return nil }
func (*mockAnalytics) Disable() error { return nil }

type mockEndpointResolver struct{ ip netip.Addr }

func newEndpointResolverMock(ip netip.Addr) mockEndpointResolver {
return mockEndpointResolver{ip: ip}
}

func (g mockEndpointResolver) Resolve(netip.Addr) ([]netip.Addr, error) {
return []netip.Addr{g.ip}, nil
}

func TestRpcConnect(t *testing.T) {
category.Set(t, category.Unit)

Expand Down Expand Up @@ -352,7 +292,7 @@ func TestRpcConnect(t *testing.T) {
dm,
api,
serversAPI,
&validCredentialsAPI{},
&testcore.CredentialsAPIMock{},
testNewCDNAPI(),
testNewRepoAPI(),
&mockAuthenticationAPI{},
Expand Down Expand Up @@ -414,7 +354,7 @@ func TestRpcReconnect(t *testing.T) {
dm,
api,
&mockServersAPI{},
&validCredentialsAPI{},
&testcore.CredentialsAPIMock{},
testNewCDNAPI(),
testNewRepoAPI(),
&mockAuthenticationAPI{},
Expand Down
4 changes: 2 additions & 2 deletions daemon/rpc_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (r *RPC) loginCommon(customCB customCallbackType) (payload *pb.LoginRespons
return pbresp, nil
}

credentials, err := r.api.ServiceCredentials(resp.Token)
credentials, err := r.credentialsAPI.ServiceCredentials(resp.Token)
if err != nil {
log.Println(internal.ErrorPrefix, "retrieving credentials:", err)
if errors.Is(err, core.ErrServerInternal) {
Expand Down Expand Up @@ -171,7 +171,7 @@ func (r *RPC) LoginOAuth2Callback(ctx context.Context, in *pb.String) (payload *
return &pb.Empty{}, err
}

credentials, err := r.api.ServiceCredentials(resp.Token)
credentials, err := r.credentialsAPI.ServiceCredentials(resp.Token)
if err != nil {
r.publisher.Publish(err.Error())
return &pb.Empty{}, err
Expand Down
4 changes: 2 additions & 2 deletions daemon/rpc_logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *RPC) Logout(ctx context.Context, in *pb.LogoutRequest) (payload *pb.Pay
log.Println(internal.WarningPrefix, "error revoking NC token")
}

if err := r.api.DeleteToken(tokenData.Token); err != nil {
if err := r.credentialsAPI.DeleteToken(tokenData.Token); err != nil {
log.Println(internal.ErrorPrefix, "deleting token: ", err)
switch {
// This means that token is invalid anyway
Expand All @@ -79,7 +79,7 @@ func (r *RPC) Logout(ctx context.Context, in *pb.LogoutRequest) (payload *pb.Pay
}
}

if err := r.api.Logout(tokenData.Token); err != nil {
if err := r.credentialsAPI.Logout(tokenData.Token); err != nil {
log.Println(internal.ErrorPrefix, "logging out: ", err)
switch {
// This means that token is invalid anyway
Expand Down
17 changes: 9 additions & 8 deletions daemon/rpc_logout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/NordSecurity/nordvpn-linux/events/subs"
"github.com/NordSecurity/nordvpn-linux/internal"
"github.com/NordSecurity/nordvpn-linux/nc"
testcore "github.com/NordSecurity/nordvpn-linux/test/mock/core"
"github.com/NordSecurity/nordvpn-linux/test/mock/networker"
testnorduser "github.com/NordSecurity/nordvpn-linux/test/mock/norduser/service"
)
Expand All @@ -34,14 +35,14 @@ func (mockApi) Logout(token string) error { return nil }

func TestLogout_Token(t *testing.T) {
rpc := RPC{
ac: &workingLoginChecker{},
cm: newMockConfigManager(),
norduser: &testnorduser.MockNorduserCombinedService{},
netw: &networker.Mock{},
ncClient: mockNC{},
publisher: &subs.Subject[string]{},
api: mockApi{},
events: &daemonevents.Events{User: &daemonevents.LoginEvents{Logout: &daemonevents.MockPublisherSubscriber[events.DataAuthorization]{}}},
ac: &workingLoginChecker{},
cm: newMockConfigManager(),
norduser: &testnorduser.MockNorduserCombinedService{},
netw: &networker.Mock{},
ncClient: mockNC{},
publisher: &subs.Subject[string]{},
credentialsAPI: &testcore.CredentialsAPIMock{},
events: &daemonevents.Events{User: &daemonevents.LoginEvents{Logout: &daemonevents.MockPublisherSubscriber[events.DataAuthorization]{}}},
}

tests := []struct {
Expand Down
4 changes: 4 additions & 0 deletions test/mock/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ func (*CredentialsAPIMock) DeleteToken(string) error {
func (*CredentialsAPIMock) TrustedPassToken(string) (*core.TrustedPassTokenResponse, error) {
return nil, nil
}

func (*CredentialsAPIMock) Logout(string) error {
return nil
}

0 comments on commit 559b83b

Please sign in to comment.