Skip to content

Commit 8e0fdbe

Browse files
authored
fix: more tests added (#8)
* fix: more tests added * fix: add chromium * fix: error handling * fix: add badges
1 parent 80cd899 commit 8e0fdbe

File tree

18 files changed

+563
-318
lines changed

18 files changed

+563
-318
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- uses: actions/checkout@v3
3939
- name: install deps
4040
run: |
41-
apt update && apt install jq -y
41+
apt-get update && apt-get install jq chromium -y
4242
make REVISION=$GITHUB_SHA install
4343
- name: make test
4444
run: |

.github/workflows/pr.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
- uses: actions/checkout@v3
4040
- name: install deps
4141
run: |
42-
apt update && apt install jq -y
42+
apt-get update && apt-get install jq chromium -y
4343
make REVISION=$GITHUB_SHA install
4444
- name: make test
4545
run: |

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ btr: build tag release
4242

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

4848
test_ci:

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
[![Bugs](https://sonarcloud.io/api/project_badges/measure?project=dnitsch_uistrategy&metric=bugs)](https://sonarcloud.io/summary/new_code?id=dnitsch_uistrategy)
2+
[![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=dnitsch_uistrategy&metric=sqale_index)](https://sonarcloud.io/summary/new_code?id=dnitsch_uistrategy)
3+
[![Reliability Rating](https://sonarcloud.io/api/project_badges/measure?project=dnitsch_uistrategy&metric=reliability_rating)](https://sonarcloud.io/summary/new_code?id=dnitsch_uistrategy)
4+
[![Vulnerabilities](https://sonarcloud.io/api/project_badges/measure?project=dnitsch_uistrategy&metric=vulnerabilities)](https://sonarcloud.io/summary/new_code?id=dnitsch_uistrategy)
5+
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=dnitsch_uistrategy&metric=coverage)](https://sonarcloud.io/summary/new_code?id=dnitsch_uistrategy)
6+
17
# UI Strategy - Beta
28

39
[![Go Report Card](https://goreportcard.com/badge/github.com/dnitsch/uistrategy)](https://goreportcard.com/report/github.com/dnitsch/uistrategy)

auth.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package uistrategy
22

3-
import "github.com/go-rod/rod"
3+
import (
4+
"fmt"
5+
6+
"github.com/go-rod/rod"
7+
)
48

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

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

5966
idpSelect, err := determinActionElement(lp.log, page, *auth.IdpSelector)
6067
if err != nil {
61-
web.log.Errorf("unable to find IdpSelector field, by selector: %v", *auth.IdpSelector.Selector)
68+
web.log.Errorf("unable to find IdpSelector field, by selector: %v, error: %v", auth.IdpSelector.Selector, err.Error())
6269
return nil, err
6370
}
6471
idpSelect.MustClick()

auth_test.go

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package uistrategy_test
2+
3+
import (
4+
"bytes"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
log "github.com/dnitsch/simplelog"
10+
"github.com/dnitsch/uistrategy"
11+
"github.com/dnitsch/uistrategy/internal/util"
12+
)
13+
14+
func TestDoLoginBasic(t *testing.T) {
15+
t.Parallel()
16+
ttests := map[string]struct {
17+
baseConf func(t *testing.T, url string) uistrategy.BaseConfig
18+
auth func(t *testing.T, url string) *uistrategy.Auth
19+
handler func(t *testing.T) http.Handler
20+
// auth func(t *testing.T, url string) *uistrategy.Auth
21+
}{
22+
"local login": {
23+
func(t *testing.T, url string) uistrategy.BaseConfig {
24+
return uistrategy.BaseConfig{
25+
BaseUrl: url,
26+
LauncherConfig: &uistrategy.WebConfig{
27+
Headless: true,
28+
},
29+
}
30+
},
31+
func(t *testing.T, url string) *uistrategy.Auth {
32+
return &uistrategy.Auth{
33+
Username: uistrategy.Element{
34+
Selector: util.Str("#username"),
35+
Value: util.Str("test"),
36+
},
37+
Password: uistrategy.Element{
38+
Selector: util.Str("#password"),
39+
Value: util.Str("test"),
40+
},
41+
Navigate: "/login",
42+
Submit: uistrategy.Element{
43+
Selector: util.Str("#submit"),
44+
},
45+
}
46+
},
47+
func(t *testing.T) http.Handler {
48+
mux := http.NewServeMux()
49+
mux.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
50+
w.Header().Set("Content-Type", "text/html; charset=utf-8")
51+
w.Write(localLoginHtml)
52+
})
53+
mux.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
54+
w.Header().Set("Content-Type", "text/html; charset=utf-8")
55+
w.Write([]byte(`<html><body>Hello!</body></html>`))
56+
})
57+
return mux
58+
},
59+
},
60+
"idp auth": {
61+
func(t *testing.T, url string) uistrategy.BaseConfig {
62+
return uistrategy.BaseConfig{
63+
BaseUrl: url,
64+
LauncherConfig: &uistrategy.WebConfig{
65+
Headless: true,
66+
},
67+
}
68+
},
69+
func(t *testing.T, url string) *uistrategy.Auth {
70+
return &uistrategy.Auth{
71+
Username: uistrategy.Element{
72+
Selector: util.Str("#username"),
73+
Value: util.Str("test"),
74+
},
75+
Password: uistrategy.Element{
76+
Selector: util.Str("#password"),
77+
Value: util.Str("test"),
78+
},
79+
Navigate: "/login-idp",
80+
IdpManaged: true,
81+
IdpSelector: &uistrategy.Element{
82+
Selector: util.Str("#idp-login"),
83+
},
84+
IdpUrl: url,
85+
Submit: uistrategy.Element{
86+
Selector: util.Str("#submit"),
87+
},
88+
}
89+
},
90+
func(t *testing.T) http.Handler {
91+
mux := http.NewServeMux()
92+
mux.HandleFunc("/login-idp", func(w http.ResponseWriter, r *http.Request) {
93+
w.Header().Set("Content-Type", "text/html; charset=utf-8")
94+
w.Write(idpLoginHtml)
95+
})
96+
mux.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
97+
w.Header().Set("Content-Type", "text/html; charset=utf-8")
98+
w.Write([]byte(`<html><body>Hello!</body></html>`))
99+
})
100+
return mux
101+
},
102+
},
103+
"mfa Login": {
104+
func(t *testing.T, url string) uistrategy.BaseConfig {
105+
return uistrategy.BaseConfig{
106+
BaseUrl: url,
107+
LauncherConfig: &uistrategy.WebConfig{
108+
Headless: true,
109+
},
110+
}
111+
},
112+
func(t *testing.T, url string) *uistrategy.Auth {
113+
return &uistrategy.Auth{
114+
Username: uistrategy.Element{
115+
Selector: util.Str("#username"),
116+
Value: util.Str("test"),
117+
},
118+
Password: uistrategy.Element{
119+
Selector: util.Str("#password"),
120+
Value: util.Str("test"),
121+
},
122+
Navigate: "/login-idp",
123+
IdpManaged: true,
124+
MfaSelector: &uistrategy.Element{
125+
Selector: util.Str("#mfa"),
126+
},
127+
IdpSelector: &uistrategy.Element{
128+
Selector: util.Str("#idp-login"),
129+
},
130+
IdpUrl: url,
131+
Submit: uistrategy.Element{
132+
Selector: util.Str("#submit"),
133+
},
134+
}
135+
},
136+
func(t *testing.T) http.Handler {
137+
mux := http.NewServeMux()
138+
mux.HandleFunc("/login-idp", func(w http.ResponseWriter, r *http.Request) {
139+
w.Header().Set("Content-Type", "text/html; charset=utf-8")
140+
w.Write(mfaLogin)
141+
})
142+
mux.HandleFunc("/app", func(w http.ResponseWriter, r *http.Request) {
143+
w.Header().Set("Content-Type", "text/html; charset=utf-8")
144+
w.Write([]byte(`<html><body>Hello!</body></html>`))
145+
})
146+
return mux
147+
},
148+
},
149+
}
150+
for name, tt := range ttests {
151+
t.Run(name, func(t *testing.T) {
152+
ts := httptest.NewServer(tt.handler(t))
153+
defer ts.Close()
154+
uiWeb := uistrategy.New(tt.baseConf(t, ts.URL)).WithLogger(log.New(&bytes.Buffer{}, log.ErrorLvl))
155+
lp, err := uiWeb.DoAuth(tt.auth(t, ts.URL))
156+
if err != nil {
157+
t.Errorf("failed to do auth: %v", err)
158+
}
159+
if lp == nil {
160+
t.Errorf("logged in page - got nil expected not nil")
161+
}
162+
})
163+
}
164+
}

cmd/uistrategy/uistrategy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var (
1818
Use: "uistrategy",
1919
RunE: runActions,
2020
Short: "executes a series of actions against a setup config",
21-
Long: ``,
21+
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. `,
2222
}
2323
)
2424

0 commit comments

Comments
 (0)