2019-03-13 04:14:30 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-11-03 11:06:11 -08:00
|
|
|
type sessionArgs struct {
|
|
|
|
userInfoSeparator string
|
|
|
|
username string
|
|
|
|
hostname string
|
|
|
|
goos string
|
|
|
|
connection string
|
|
|
|
client string
|
|
|
|
sshIcon string
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupSession(args *sessionArgs) session {
|
2019-03-13 04:14:30 -07:00
|
|
|
env := new(MockedEnvironment)
|
2020-11-03 11:06:11 -08:00
|
|
|
env.On("getCurrentUser", nil).Return(args.username)
|
|
|
|
env.On("getHostName", nil).Return(args.hostname, nil)
|
|
|
|
env.On("getRuntimeGOOS", nil).Return(args.goos)
|
|
|
|
env.On("getenv", "SSH_CONNECTION").Return(args.connection)
|
|
|
|
env.On("getenv", "SSH_CLIENT").Return(args.client)
|
2019-03-13 04:14:30 -07:00
|
|
|
props := &properties{
|
2020-11-03 11:06:11 -08:00
|
|
|
values: map[Property]interface{}{
|
|
|
|
UserInfoSeparator: args.userInfoSeparator,
|
|
|
|
SSHIcon: args.sshIcon,
|
|
|
|
},
|
2019-03-13 04:14:30 -07:00
|
|
|
foreground: "#fff",
|
|
|
|
background: "#000",
|
|
|
|
}
|
|
|
|
s := session{
|
|
|
|
env: env,
|
|
|
|
props: props,
|
|
|
|
}
|
2020-10-07 08:14:26 -07:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-11-03 11:06:11 -08:00
|
|
|
func testUserInfoWriter(args *sessionArgs) string {
|
|
|
|
s := setupSession(args)
|
2020-12-02 10:00:46 -08:00
|
|
|
_ = s.enabled()
|
2019-03-13 04:14:30 -07:00
|
|
|
return s.getFormattedText()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteUserInfo(t *testing.T) {
|
|
|
|
want := "<#fff>bill</>@<#fff>surface</>"
|
2020-11-03 11:06:11 -08:00
|
|
|
args := &sessionArgs{
|
|
|
|
userInfoSeparator: "@",
|
|
|
|
username: "bill",
|
|
|
|
hostname: "surface",
|
|
|
|
goos: "windows",
|
|
|
|
}
|
|
|
|
got := testUserInfoWriter(args)
|
2019-03-13 04:14:30 -07:00
|
|
|
assert.EqualValues(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteUserInfoWindowsIncludingHostname(t *testing.T) {
|
|
|
|
want := "<#fff>bill</>@<#fff>surface</>"
|
2020-11-03 11:06:11 -08:00
|
|
|
args := &sessionArgs{
|
|
|
|
userInfoSeparator: "@",
|
|
|
|
username: "surface\\bill",
|
|
|
|
hostname: "surface",
|
|
|
|
goos: "windows",
|
|
|
|
}
|
|
|
|
got := testUserInfoWriter(args)
|
2019-03-13 04:14:30 -07:00
|
|
|
assert.EqualValues(t, want, got)
|
|
|
|
}
|
2020-10-07 08:14:26 -07:00
|
|
|
|
|
|
|
func TestWriteOnlyUsername(t *testing.T) {
|
2020-11-03 11:06:11 -08:00
|
|
|
args := &sessionArgs{
|
|
|
|
userInfoSeparator: "@",
|
|
|
|
username: "surface\\bill",
|
|
|
|
hostname: "surface",
|
|
|
|
goos: "windows",
|
|
|
|
}
|
|
|
|
s := setupSession(args)
|
2020-10-07 08:14:26 -07:00
|
|
|
s.props.values[DisplayHost] = false
|
|
|
|
want := "<#fff>bill</><#fff></>"
|
2020-12-02 10:00:46 -08:00
|
|
|
assert.True(t, s.enabled())
|
2020-10-07 08:14:26 -07:00
|
|
|
got := s.getFormattedText()
|
|
|
|
assert.EqualValues(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWriteOnlyHostname(t *testing.T) {
|
2020-11-03 11:06:11 -08:00
|
|
|
args := &sessionArgs{
|
|
|
|
userInfoSeparator: "@",
|
|
|
|
username: "surface\\bill",
|
|
|
|
hostname: "surface",
|
|
|
|
goos: "windows",
|
|
|
|
}
|
|
|
|
s := setupSession(args)
|
2020-10-07 08:14:26 -07:00
|
|
|
s.props.values[DisplayUser] = false
|
|
|
|
want := "<#fff></><#fff>surface</>"
|
2020-12-02 10:00:46 -08:00
|
|
|
assert.True(t, s.enabled())
|
2020-10-07 08:14:26 -07:00
|
|
|
got := s.getFormattedText()
|
|
|
|
assert.EqualValues(t, want, got)
|
|
|
|
}
|
2020-10-12 03:53:54 -07:00
|
|
|
|
2020-11-03 11:06:11 -08:00
|
|
|
func TestWriteActiveSSHSession(t *testing.T) {
|
|
|
|
want := "ssh <#fff>bill</>@<#fff>surface</>"
|
|
|
|
args := &sessionArgs{
|
|
|
|
userInfoSeparator: "@",
|
|
|
|
username: "bill",
|
|
|
|
hostname: "surface",
|
|
|
|
goos: "windows",
|
|
|
|
sshIcon: "ssh ",
|
|
|
|
connection: "1.1.1.1",
|
|
|
|
}
|
|
|
|
got := testUserInfoWriter(args)
|
|
|
|
assert.EqualValues(t, want, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActiveSSHSessionInactive(t *testing.T) {
|
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("getenv", "SSH_CONNECTION").Return("")
|
|
|
|
env.On("getenv", "SSH_CLIENT").Return("")
|
|
|
|
s := &session{
|
|
|
|
env: env,
|
|
|
|
}
|
|
|
|
assert.False(t, s.activeSSHSession())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActiveSSHSessionActiveConnection(t *testing.T) {
|
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("getenv", "SSH_CONNECTION").Return("1.1.1.1")
|
|
|
|
env.On("getenv", "SSH_CLIENT").Return("")
|
|
|
|
s := &session{
|
|
|
|
env: env,
|
|
|
|
}
|
|
|
|
assert.True(t, s.activeSSHSession())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActiveSSHSessionActiveClient(t *testing.T) {
|
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("getenv", "SSH_CONNECTION").Return("")
|
|
|
|
env.On("getenv", "SSH_CLIENT").Return("1.1.1.1")
|
|
|
|
s := &session{
|
|
|
|
env: env,
|
|
|
|
}
|
|
|
|
assert.True(t, s.activeSSHSession())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActiveSSHSessionActiveBoth(t *testing.T) {
|
|
|
|
env := new(MockedEnvironment)
|
|
|
|
env.On("getenv", "SSH_CONNECTION").Return("2.2.2.2")
|
|
|
|
env.On("getenv", "SSH_CLIENT").Return("1.1.1.1")
|
2020-10-12 03:53:54 -07:00
|
|
|
s := &session{
|
2020-11-03 11:06:11 -08:00
|
|
|
env: env,
|
2020-10-12 03:53:54 -07:00
|
|
|
}
|
2020-11-03 11:06:11 -08:00
|
|
|
assert.True(t, s.activeSSHSession())
|
2020-10-12 03:53:54 -07:00
|
|
|
}
|