Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Add more tests for appsync (#1760)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo authored Feb 3, 2021
1 parent 3a665a6 commit 191468f
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 138 deletions.
72 changes: 1 addition & 71 deletions pkg/controller/appsync/appsync_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Google LLC
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,9 +17,6 @@ package appsync
import (
"testing"

"github.com/google/exposure-notifications-verification-server/internal/clients"
"github.com/google/exposure-notifications-verification-server/internal/project"
"github.com/google/exposure-notifications-verification-server/pkg/config"
"github.com/google/exposure-notifications-verification-server/pkg/database"
)

Expand All @@ -30,70 +27,3 @@ func TestMain(m *testing.M) {
defer testDatabaseInstance.MustClose()
m.Run()
}

func TestAppSync(t *testing.T) {
t.Parallel()

ctx := project.TestContext(t)

db, _ := testDatabaseInstance.NewDatabase(t, nil)
config := &config.AppSyncConfig{}
c, _ := New(config, db, nil)

t.Run("name", func(t *testing.T) {
t.Parallel()

realm := database.NewRealmWithDefaults("test")
realm.RegionCode = "US-WA"
if err := db.SaveRealm(realm, database.SystemTest); err != nil {
t.Fatalf("error saving realm: %v", err)
}

m := &database.MobileApp{
Name: "US-WA Android App",
RealmID: realm.ID,
OS: database.OSTypeAndroid,
AppID: "testAppId",
SHA: "AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA",
}
if err := db.SaveMobileApp(m, database.SystemTest); err != nil {
t.Fatalf("error saving realm: %v", err)
}

resp := &clients.AppsResponse{
Apps: []clients.App{
{
Region: "US-WA",
IsEnx: true,
AndroidTarget: clients.AndroidTarget{
Namespace: "android_app",
PackageName: "testAppID-butDifferent",
SHA256CertFingerprints: "AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA",
},
}, {
Region: "US-WA",
IsEnx: true,
AndroidTarget: clients.AndroidTarget{
Namespace: "android_app",
PackageName: "testAppId2",
SHA256CertFingerprints: "BB:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA",
},
},
},
}

merr := c.syncApps(ctx, resp)
if e := merr.ErrorOrNil(); e != nil {
t.Fatalf(e.Error())
}

apps, err := db.ListActiveApps(realm.ID)
if err != nil {
t.Fatal("failed to list apps", err)
}

if got, want := len(apps), 2; got != want {
t.Errorf("got %d apps, expected %d", got, want)
}
})
}
10 changes: 5 additions & 5 deletions pkg/controller/appsync/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ type Controller struct {
}

// New creates a new appsync controller.
func New(config *config.AppSyncConfig, db *database.Database, h render.Renderer) (*Controller, error) {
appSyncClient, err := clients.NewAppSyncClient(config.AppSyncURL,
clients.WithTimeout(config.Timeout),
clients.WithMaxBodySize(config.FileSizeLimitBytes))
func New(cfg *config.AppSyncConfig, db *database.Database, h render.Renderer) (*Controller, error) {
appSyncClient, err := clients.NewAppSyncClient(cfg.AppSyncURL,
clients.WithTimeout(cfg.Timeout),
clients.WithMaxBodySize(cfg.FileSizeLimitBytes))
if err != nil {
return nil, err
}

return &Controller{
config: config,
config: cfg,
db: db,
h: h,

Expand Down
33 changes: 33 additions & 0 deletions pkg/controller/appsync/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package appsync

import (
"testing"

"github.com/google/exposure-notifications-verification-server/pkg/config"
)

func TestNew(t *testing.T) {
t.Parallel()

cfg := &config.AppSyncConfig{
AppSyncURL: "totally invalid" + string(rune(0x7f)),
}

if _, err := New(cfg, nil, nil); err == nil {
t.Errorf("expected error")
}
}
74 changes: 74 additions & 0 deletions pkg/controller/appsync/handle_sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package appsync

import (
"net/http"

"github.com/google/exposure-notifications-server/pkg/logging"

"github.com/google/exposure-notifications-verification-server/internal/project"
"github.com/google/exposure-notifications-verification-server/pkg/controller"
)

// HandleSync performs the logic to sync mobile apps.
func (c *Controller) HandleSync() http.Handler {
type AppSyncResult struct {
OK bool `json:"ok"`
Errors []string `json:"errors,omitempty"`
}

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

logger := logging.FromContext(ctx).Named("appsync.HandleSync")

ok, err := c.db.TryLock(ctx, appSyncLock, c.config.AppSyncMinPeriod)
if err != nil {
logger.Errorw("failed to acquire lock", "error", err)
c.h.RenderJSON(w, http.StatusInternalServerError, &AppSyncResult{
OK: false,
Errors: []string{err.Error()},
})
return
}
if !ok {
logger.Debugw("skipping (too early)")
c.h.RenderJSON(w, http.StatusOK, &AppSyncResult{
OK: false,
Errors: []string{"too early"},
})
return
}

apps, err := c.appSyncClient.AppSync(ctx)
if err != nil {
controller.InternalError(w, r, c.h, err)
return
}

// If there are any errors, return them
if merr := c.syncApps(ctx, apps); merr != nil {
if errs := merr.WrappedErrors(); len(errs) > 0 {
c.h.RenderJSON(w, http.StatusInternalServerError, &AppSyncResult{
OK: false,
Errors: project.ErrorsToStrings(errs),
})
return
}
}
c.h.RenderJSON(w, http.StatusOK, &AppSyncResult{OK: true})
})
}
Loading

0 comments on commit 191468f

Please sign in to comment.