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

Remove unneeded chromedp from realmadmin and realmkeys #1871

Merged
merged 2 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions builders/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ steps:
- '-o=./bin/'
- './cmd/...'
waitFor:
- 'restore-cache'
- 'mkdir-bin'

- id: 'save-cache'
Expand Down
84 changes: 27 additions & 57 deletions pkg/controller/mobileapps/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,19 @@
package mobileapps_test

import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"

"github.com/google/exposure-notifications-verification-server/internal/browser"
"github.com/google/exposure-notifications-verification-server/internal/envstest"
"github.com/google/exposure-notifications-verification-server/internal/project"
"github.com/google/exposure-notifications-verification-server/pkg/controller"
"github.com/google/exposure-notifications-verification-server/pkg/controller/mobileapps"
"github.com/google/exposure-notifications-verification-server/pkg/database"
"github.com/google/exposure-notifications-verification-server/pkg/rbac"
"github.com/gorilla/sessions"

"github.com/chromedp/chromedp"
)

func TestHandleCreate(t *testing.T) {
Expand All @@ -41,22 +36,17 @@ func TestHandleCreate(t *testing.T) {
ctx := project.TestContext(t)
harness := envstest.NewServer(t, testDatabaseInstance)

realm, user, session, err := harness.ProvisionAndLogin()
realm, user, _, err := harness.ProvisionAndLogin()
if err != nil {
t.Fatal(err)
}

cookie, err := harness.SessionCookie(session)
if err != nil {
t.Fatal(err)
}
c := mobileapps.New(harness.Database, harness.Renderer)
handler := c.HandleCreate()

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

c := mobileapps.New(harness.Database, harness.Renderer)
handler := c.HandleCreate()

envstest.ExerciseSessionMissing(t, handler)
envstest.ExerciseMembershipMissing(t, handler)
envstest.ExercisePermissionMissing(t, handler)
Expand All @@ -65,10 +55,7 @@ func TestHandleCreate(t *testing.T) {
t.Run("internal_error", func(t *testing.T) {
t.Parallel()

harness := envstest.NewServerConfig(t, testDatabaseInstance)
harness.Database.SetRawDB(envstest.NewFailingDatabase())

c := mobileapps.New(harness.Database, harness.Renderer)
c := mobileapps.New(harness.BadDatabase, harness.Renderer)
handler := c.HandleCreate()

ctx := ctx
Expand All @@ -79,35 +66,22 @@ func TestHandleCreate(t *testing.T) {
Permissions: rbac.MobileAppWrite,
})

r := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(url.Values{
w, r := envstest.BuildFormRequest(ctx, t, http.MethodPost, "/", &url.Values{
"name": []string{"banana"},
"url": []string{"http://example.com"},
"os": []string{"1"},
"app_id": []string{"com.example.app"},
}.Encode()))
r = r.Clone(ctx)
r.Header.Set("Accept", "text/html")
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

w := httptest.NewRecorder()

})
handler.ServeHTTP(w, r)
w.Flush()

if got, want := w.Code, http.StatusInternalServerError; got != want {
t.Errorf("Expected %d to be %d", got, want)
}
if got, want := w.Body.String(), "Internal server error"; !strings.Contains(got, want) {
t.Errorf("Expected %q to contain %q", got, want)
}
})

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

c := mobileapps.New(harness.Database, harness.Renderer)
handler := c.HandleCreate()

ctx := ctx
ctx = controller.WithSession(ctx, &sessions.Session{})
ctx = controller.WithMembership(ctx, &database.Membership{
Expand All @@ -116,15 +90,8 @@ func TestHandleCreate(t *testing.T) {
Permissions: rbac.MobileAppWrite,
})

r := httptest.NewRequest(http.MethodPost, "/", nil)
r = r.Clone(ctx)
r.Header.Set("Accept", "text/html")
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

w := httptest.NewRecorder()

w, r := envstest.BuildFormRequest(ctx, t, http.MethodPost, "/", &url.Values{})
handler.ServeHTTP(w, r)
w.Flush()

if got, want := w.Code, http.StatusUnprocessableEntity; got != want {
t.Errorf("Expected %d to be %d", got, want)
Expand All @@ -134,33 +101,36 @@ func TestHandleCreate(t *testing.T) {
}
})

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

browserCtx := browser.New(t)
taskCtx, done := context.WithTimeout(browserCtx, project.TestTimeout())
defer done()

if err := chromedp.Run(taskCtx,
browser.SetCookie(cookie),
chromedp.Navigate(`http://`+harness.Server.Addr()+`/realm/mobile-apps/new`),
ctx := ctx
ctx = controller.WithSession(ctx, &sessions.Session{})
ctx = controller.WithMembership(ctx, &database.Membership{
Realm: realm,
User: user,
Permissions: rbac.MobileAppWrite,
})

chromedp.SetValue(`input#name`, "Example mobile app", chromedp.ByQuery),
chromedp.SetValue(`input#url`, "https://example.com", chromedp.ByQuery),
chromedp.SetValue(`select#os`, strconv.Itoa(int(database.OSTypeIOS)), chromedp.ByQuery),
chromedp.SetValue(`input#app-id`, "com.example.app", chromedp.ByQuery),
chromedp.Click(`#submit`, chromedp.ByQuery),
w, r := envstest.BuildFormRequest(ctx, t, http.MethodPost, "/", &url.Values{
"name": []string{"Example mobile app"},
"url": []string{"https://example.com"},
"os": []string{strconv.Itoa(int(database.OSTypeIOS))},
"app_id": []string{"com.example.app"},
})
handler.ServeHTTP(w, r)

chromedp.WaitVisible(`body#mobileapps-show`, chromedp.ByQuery),
); err != nil {
t.Fatal(err)
if got, want := w.Code, http.StatusSeeOther; got != want {
t.Errorf("expected %d to be %d: %s", got, want, w.Body.String())
}
if got, want := w.Header().Get("Location"), "/realm/mobile-apps/1"; got != want {
t.Errorf("expected %q to be %q", got, want)
}

record, err := realm.FindMobileApp(harness.Database, 1)
if err != nil {
t.Fatal(err)
}

if got, want := record.RealmID, realm.ID; got != want {
t.Errorf("expected %v to be %v", got, want)
}
Expand Down
73 changes: 23 additions & 50 deletions pkg/controller/mobileapps/disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@
package mobileapps_test

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/chromedp/chromedp"
"github.com/google/exposure-notifications-verification-server/internal/browser"
"github.com/google/exposure-notifications-verification-server/internal/envstest"
"github.com/google/exposure-notifications-verification-server/internal/project"
"github.com/google/exposure-notifications-verification-server/pkg/controller"
Expand All @@ -40,22 +35,17 @@ func TestHandleDisable(t *testing.T) {
ctx := project.TestContext(t)
harness := envstest.NewServer(t, testDatabaseInstance)

realm, user, session, err := harness.ProvisionAndLogin()
realm, user, _, err := harness.ProvisionAndLogin()
if err != nil {
t.Fatal(err)
}

cookie, err := harness.SessionCookie(session)
if err != nil {
t.Fatal(err)
}
c := mobileapps.New(harness.Database, harness.Renderer)
handler := c.HandleDisable()

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

c := mobileapps.New(harness.Database, harness.Renderer)
handler := c.HandleDisable()

envstest.ExerciseSessionMissing(t, handler)
envstest.ExerciseMembershipMissing(t, handler)
envstest.ExercisePermissionMissing(t, handler)
Expand All @@ -69,13 +59,8 @@ func TestHandleDisable(t *testing.T) {
t.Run("internal_error", func(t *testing.T) {
t.Parallel()

harness := envstest.NewServerConfig(t, testDatabaseInstance)
harness.Database.SetRawDB(envstest.NewFailingDatabase())

c := mobileapps.New(harness.Database, harness.Renderer)

mux := mux.NewRouter()
mux.Handle("/{id}", c.HandleDisable()).Methods(http.MethodPut)
c := mobileapps.New(harness.BadDatabase, harness.Renderer)
handler := c.HandleDisable()

ctx := ctx
ctx = controller.WithSession(ctx, &sessions.Session{})
Expand All @@ -85,24 +70,16 @@ func TestHandleDisable(t *testing.T) {
Permissions: rbac.MobileAppWrite,
})

r := httptest.NewRequest(http.MethodPut, "/1", nil)
r = r.Clone(ctx)
r.Header.Set("Content-Type", "text/html")

w := httptest.NewRecorder()

mux.ServeHTTP(w, r)
w.Flush()
w, r := envstest.BuildFormRequest(ctx, t, http.MethodPut, "/", nil)
r = mux.SetURLVars(r, map[string]string{"id": "1"})
handler.ServeHTTP(w, r)

if got, want := w.Code, http.StatusInternalServerError; got != want {
t.Errorf("Expected %d to be %d", got, want)
}
if got, want := w.Body.String(), "Internal server error"; !strings.Contains(got, want) {
t.Errorf("Expected %q to contain %q", got, want)
}
})

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

app := &database.MobileApp{
Expand All @@ -116,27 +93,23 @@ func TestHandleDisable(t *testing.T) {
t.Fatal(err)
}

browserCtx := browser.New(t)
taskCtx, done := context.WithTimeout(browserCtx, project.TestTimeout())
defer done()

// Click "confirm" when it pops up.
confirmErrCh := envstest.AutoConfirmDialogs(taskCtx, true)

if err := chromedp.Run(taskCtx,
browser.SetCookie(cookie),
chromedp.Navigate(`http://`+harness.Server.Addr()+`/realm/mobile-apps`),
chromedp.WaitVisible(`body#mobileapps-index`, chromedp.ByQuery),
ctx := ctx
ctx = controller.WithSession(ctx, &sessions.Session{})
ctx = controller.WithMembership(ctx, &database.Membership{
Realm: realm,
User: user,
Permissions: rbac.MobileAppWrite,
})

chromedp.Click(fmt.Sprintf(`a#disable-mobileapp-%d`, app.ID), chromedp.ByQuery),
w, r := envstest.BuildFormRequest(ctx, t, http.MethodPut, "/", nil)
r = mux.SetURLVars(r, map[string]string{"id": fmt.Sprintf("%d", app.ID)})
handler.ServeHTTP(w, r)

chromedp.WaitVisible(`body#mobileapps-index`, chromedp.ByQuery),
); err != nil {
t.Fatal(err)
if got, want := w.Code, http.StatusSeeOther; got != want {
t.Errorf("expected %d to be %d: %s", got, want, w.Body.String())
}

if err := <-confirmErrCh; err != nil {
t.Fatal(err)
if got, want := w.Header().Get("Location"), "/realm/mobile-apps"; got != want {
t.Errorf("expected %s to be %s", got, want)
}

record, err := realm.FindMobileApp(harness.Database, app.ID)
Expand Down
Loading