Skip to content

Commit

Permalink
fix: more tests added (#8)
Browse files Browse the repository at this point in the history
* fix: more tests added

* fix: add chromium

* fix: error handling

* fix: add badges
  • Loading branch information
dnitsch authored Mar 12, 2023
1 parent 80cd899 commit 8e0fdbe
Show file tree
Hide file tree
Showing 18 changed files with 563 additions and 318 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- uses: actions/checkout@v3
- name: install deps
run: |
apt update && apt install jq -y
apt-get update && apt-get install jq chromium -y
make REVISION=$GITHUB_SHA install
- name: make test
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- uses: actions/checkout@v3
- name: install deps
run: |
apt update && apt install jq -y
apt-get update && apt-get install jq chromium -y
make REVISION=$GITHUB_SHA install
- name: make test
run: |
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ btr: build tag release

# TEST
test: test_prereq
go test `go list ./... | grep -v */generated/` -v -mod=readonly -race -coverprofile=.coverage/out | go-junit-report > .coverage/report-junit.xml && \
go test ./... -v -mod=readonly -race -coverprofile=.coverage/out | go-junit-report > .coverage/report-junit.xml && \
gocov convert .coverage/out | gocov-xml > .coverage/report-cobertura.xml

test_ci:
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=dnitsch_uistrategy&metric=bugs)](https://sonarcloud.io/summary/new_code?id=dnitsch_uistrategy)
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=dnitsch_uistrategy&metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=dnitsch_uistrategy)
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=dnitsch_uistrategy&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=dnitsch_uistrategy)
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=dnitsch_uistrategy&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=dnitsch_uistrategy)
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=dnitsch_uistrategy&metric=coverage)](https://sonarcloud.io/summary/new_code?id=dnitsch_uistrategy)

# UI Strategy - Beta

[![Go Report Card](https://goreportcard.com/badge/github.com/dnitsch/uistrategy)](https://goreportcard.com/report/github.com/dnitsch/uistrategy)
Expand Down
11 changes: 9 additions & 2 deletions auth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package uistrategy

import "github.com/go-rod/rod"
import (
"fmt"

"github.com/go-rod/rod"
)

type Auth struct {
Username Element `yaml:"username" json:"username"`
Expand Down Expand Up @@ -50,6 +54,9 @@ func (web *Web) doLocalAuth(auth Auth) (*LoggedInPage, error) {
// SP initiated will be simpler as you can omit the idpUrl
// the flow will follow redirects
func (web *Web) doIdpAuth(auth Auth) (*LoggedInPage, error) {
if auth.IdpSelector == nil {
return nil, fmt.Errorf("idpSelector must be specified")
}

page := web.browser.MustPage(web.config.BaseUrl + auth.Navigate).MustWaitLoad()
lp := &LoggedInPage{web, page, UIStrategyError{}}
Expand All @@ -58,7 +65,7 @@ func (web *Web) doIdpAuth(auth Auth) (*LoggedInPage, error) {

idpSelect, err := determinActionElement(lp.log, page, *auth.IdpSelector)
if err != nil {
web.log.Errorf("unable to find IdpSelector field, by selector: %v", *auth.IdpSelector.Selector)
web.log.Errorf("unable to find IdpSelector field, by selector: %v, error: %v", auth.IdpSelector.Selector, err.Error())
return nil, err
}
idpSelect.MustClick()
Expand Down
164 changes: 164 additions & 0 deletions auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package uistrategy_test

import (
"bytes"
"net/http"
"net/http/httptest"
"testing"

log "github.com/dnitsch/simplelog"
"github.com/dnitsch/uistrategy"
"github.com/dnitsch/uistrategy/internal/util"
)

func TestDoLoginBasic(t *testing.T) {
t.Parallel()
ttests := map[string]struct {
baseConf func(t *testing.T, url string) uistrategy.BaseConfig
auth func(t *testing.T, url string) *uistrategy.Auth
handler func(t *testing.T) http.Handler
// auth func(t *testing.T, url string) *uistrategy.Auth
}{
"local login": {
func(t *testing.T, url string) uistrategy.BaseConfig {
return uistrategy.BaseConfig{
BaseUrl: url,
LauncherConfig: &uistrategy.WebConfig{
Headless: true,
},
}
},
func(t *testing.T, url string) *uistrategy.Auth {
return &uistrategy.Auth{
Username: uistrategy.Element{
Selector: util.Str("#username"),
Value: util.Str("test"),
},
Password: uistrategy.Element{
Selector: util.Str("#password"),
Value: util.Str("test"),
},
Navigate: "/login",
Submit: uistrategy.Element{
Selector: util.Str("#submit"),
},
}
},
func(t *testing.T) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(localLoginHtml)
})
mux.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(`<html><body>Hello!</body></html>`))
})
return mux
},
},
"idp auth": {
func(t *testing.T, url string) uistrategy.BaseConfig {
return uistrategy.BaseConfig{
BaseUrl: url,
LauncherConfig: &uistrategy.WebConfig{
Headless: true,
},
}
},
func(t *testing.T, url string) *uistrategy.Auth {
return &uistrategy.Auth{
Username: uistrategy.Element{
Selector: util.Str("#username"),
Value: util.Str("test"),
},
Password: uistrategy.Element{
Selector: util.Str("#password"),
Value: util.Str("test"),
},
Navigate: "/login-idp",
IdpManaged: true,
IdpSelector: &uistrategy.Element{
Selector: util.Str("#idp-login"),
},
IdpUrl: url,
Submit: uistrategy.Element{
Selector: util.Str("#submit"),
},
}
},
func(t *testing.T) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/login-idp", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(idpLoginHtml)
})
mux.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(`<html><body>Hello!</body></html>`))
})
return mux
},
},
"mfa Login": {
func(t *testing.T, url string) uistrategy.BaseConfig {
return uistrategy.BaseConfig{
BaseUrl: url,
LauncherConfig: &uistrategy.WebConfig{
Headless: true,
},
}
},
func(t *testing.T, url string) *uistrategy.Auth {
return &uistrategy.Auth{
Username: uistrategy.Element{
Selector: util.Str("#username"),
Value: util.Str("test"),
},
Password: uistrategy.Element{
Selector: util.Str("#password"),
Value: util.Str("test"),
},
Navigate: "/login-idp",
IdpManaged: true,
MfaSelector: &uistrategy.Element{
Selector: util.Str("#mfa"),
},
IdpSelector: &uistrategy.Element{
Selector: util.Str("#idp-login"),
},
IdpUrl: url,
Submit: uistrategy.Element{
Selector: util.Str("#submit"),
},
}
},
func(t *testing.T) http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/login-idp", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write(mfaLogin)
})
mux.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(`<html><body>Hello!</body></html>`))
})
return mux
},
},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
ts := httptest.NewServer(tt.handler(t))
defer ts.Close()
uiWeb := uistrategy.New(tt.baseConf(t, ts.URL)).WithLogger(log.New(&bytes.Buffer{}, log.ErrorLvl))
lp, err := uiWeb.DoAuth(tt.auth(t, ts.URL))
if err != nil {
t.Errorf("failed to do auth: %v", err)
}
if lp == nil {
t.Errorf("logged in page - got nil expected not nil")
}
})
}
}
2 changes: 1 addition & 1 deletion cmd/uistrategy/uistrategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
Use: "uistrategy",
RunE: runActions,
Short: "executes a series of actions against a setup config",
Long: ``,
Long: `executes a series of instructions against a any number of paths under the same host. supports multiple login options - basic/Idp/MFA e.g. `,
}
)

Expand Down
Loading

0 comments on commit 8e0fdbe

Please sign in to comment.