diff options
| -rw-r--r-- | ci/docker/go-test.docker | 26 | ||||
| -rw-r--r-- | selenium_eduvpn.py | 30 | ||||
| -rw-r--r-- | src/server_test.go | 54 |
3 files changed, 109 insertions, 1 deletions
diff --git a/ci/docker/go-test.docker b/ci/docker/go-test.docker index d576dd7..9e6ad3a 100644 --- a/ci/docker/go-test.docker +++ b/ci/docker/go-test.docker @@ -1,11 +1,35 @@ FROM golang:1.18 +WORKDIR /eduvpn + + +# Selenium dependencies + +# Firefox +RUN echo "deb http://deb.debian.org/debian/ unstable main contrib non-free" >> /etc/apt/sources.list.d/debian.list +RUN apt-get update +RUN apt-get -y install openjdk-11-jre xvfb python3-selenium firefox python3-pyvirtualdisplay + +# Install geckodriver and add to path +WORKDIR /eduvpn/go/vendor + +RUN wget https://github.com/mozilla/geckodriver/releases/download/v0.30.0/geckodriver-v0.30.0-linux64.tar.gz +RUN tar xzvf geckodriver-v0.30.0-linux64.tar.gz + +ENV PATH="/eduvpn/go/vendor:$PATH" + WORKDIR /eduvpn/go # pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change COPY ./go.mod go.sum ./ RUN go mod download && go mod verify +WORKDIR /eduvpn/go + +# Copy go source COPY ./src ./src -CMD ["go", "test", "github.com/jwijenbergh/eduvpn-common/src"] +# Copy selenium scripts +COPY ./selenium_eduvpn.py ./selenium_eduvpn.py + +CMD ["go", "test", "-mod=readonly", "github.com/jwijenbergh/eduvpn-common/src", "-v"] diff --git a/selenium_eduvpn.py b/selenium_eduvpn.py new file mode 100644 index 0000000..08b48ef --- /dev/null +++ b/selenium_eduvpn.py @@ -0,0 +1,30 @@ +import sys +from selenium import webdriver +from selenium.webdriver.common.keys import Keys +from pyvirtualdisplay import Display + +def login_oauth(driver, authURL): + driver.get(authURL) + assert "VPN Portal - Sign In" in driver.title + elem = driver.find_element_by_name("userName") + elem.clear() + elem.send_keys("docker") + + elem = driver.find_element_by_name("userPass") + elem.clear() + elem.send_keys("docker") + driver.find_element_by_css_selector('.frm > fieldset:nth-child(2) > button:nth-child(2)').click() + assert "VPN Portal - Approve Application" in driver.title + driver.find_element_by_css_selector('.frm > fieldset:nth-child(1) > button:nth-child(1)').click() + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("Error: no auth url specified") + sys.exit(1) + disp = Display() + disp.start() + driver = webdriver.Firefox() + authURL = sys.argv[1] + login_oauth(driver, authURL) + driver.close() + disp.stop() diff --git a/src/server_test.go b/src/server_test.go new file mode 100644 index 0000000..54c7809 --- /dev/null +++ b/src/server_test.go @@ -0,0 +1,54 @@ +package eduvpn + +import ( + "testing" + "net/http" + "crypto/tls" + "os/exec" + "strings" +) + +func RunCommand(t *testing.T, name string, args ...string) { + cmd := exec.Command(name, args...) + var errBuffer strings.Builder + + cmd.Stderr = &errBuffer + err := cmd.Start() + if err != nil { + t.Errorf("%v", err) + } + + err = cmd.Wait() + + if err != nil { + t.Errorf("Login OAuth with selenium script failed with error %v and stderr %s", err, errBuffer.String()) + } +} + +func LoginOAuthSelenium(t* testing.T, url string) { + // We could use the go selenium library + // But it does not support the latest selenium v4 just yet + RunCommand(t, "python3", "../selenium_eduvpn.py", url) +} + +func StateCallback(t *testing.T, oldState string, newState string, data string) { + if newState == "OAuthInitialized" { + LoginOAuthSelenium(t, data) + } +} + +func TestServer(t *testing.T) { + state := GetVPNState() + + // Do not verify because during testing, the cert is self-signed + http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} + + state.Register("org.eduvpn.app.linux", "configs", func(old string, new string, data string) { + StateCallback(t, old, new, data) + }) + _, configErr := state.Connect("https://eduvpnserver") + + if configErr != nil { + t.Errorf("Connect error: %v", configErr) + } +} |
