Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing/client tests #573

Merged
merged 9 commits into from
Feb 16, 2019
83 changes: 83 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,24 @@ func TestDaemonUp(t *testing.T) {
assert.Equal(t, actualCommand, session.Calls[0])
}

func TestDaemonDown(t *testing.T) {
session := &mockSSHRunner{}
client := newMockSSHClient(session)
client.version = "latest"
client.IP = "0.0.0.0"
client.Daemon.Port = "4303"
script, err := ioutil.ReadFile("scripts/daemon-up.sh")
assert.Nil(t, err)
actualCommand := fmt.Sprintf(string(script), "latest", "4303", "0.0.0.0")

// Make sure the right command is run.
err = client.DaemonUp("latest")
err = client.DaemonDown()
assert.Nil(t, err)
println(actualCommand)
assert.Equal(t, actualCommand, session.Calls[0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't this actually be testing daemon-up.sh? see line 139 πŸ€”

}

func TestKeyGen(t *testing.T) {
session := &mockSSHRunner{}
remote := newMockSSHClient(session)
Expand Down Expand Up @@ -561,3 +579,68 @@ func TestLogIn(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestEnableTotp(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)

// Check request method
assert.Equal(t, "POST", req.Method)

// Check correct endpoint called
endpoint := req.URL.Path
assert.Equal(t, "/user/totp/enable", endpoint)

// Check auth
assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization"))
}))
defer testServer.Close()

d := newMockClient(testServer)
resp, err := d.EnableTotp("", "")
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestDisableTotp(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)

// Check request method
assert.Equal(t, "POST", req.Method)

// Check correct endpoint called
endpoint := req.URL.Path
assert.Equal(t, "/user/totp/disable", endpoint)

// Check auth
assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization"))
}))
defer testServer.Close()

d := newMockClient(testServer)
resp, err := d.DisableTotp()
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}

func TestSetSSLVerification(t *testing.T) {
testServer := httptest.NewTLSServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusOK)

// Check request method
assert.Equal(t, "POST", req.Method)

// Check correct endpoint called
endpoint := req.URL.Path
assert.Equal(t, "/user/totp/disable", endpoint)

// Check auth
assert.Equal(t, "Bearer "+fakeAuth, req.Header.Get("Authorization"))
}))
defer testServer.Close()

d := newMockClient(testServer)
d.SetSSLVerification(true)
assert.Equal(t, d.verifySSL, true)
}