Skip to content

Commit

Permalink
client: additional tests (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoharry authored and bobheadxi committed Feb 16, 2019
1 parent 3ed987b commit 8ab04a5
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ 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-down.sh")
assert.Nil(t, err)
actualCommand := fmt.Sprintf(string(script))

// Make sure the right command is run.
err = client.DaemonDown()
assert.Nil(t, err)
println(actualCommand)
assert.Equal(t, actualCommand, session.Calls[0])
}

func TestKeyGen(t *testing.T) {
session := &mockSSHRunner{}
remote := newMockSSHClient(session)
Expand Down Expand Up @@ -561,3 +578,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)
}

0 comments on commit 8ab04a5

Please sign in to comment.