-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
201 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
//nolint:lll // long config samples | ||
package tool | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/x/exp/teatest" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestTool_StartNoEndpointFailedConnect(t *testing.T) { | ||
tool, err := New() | ||
require.NoError(t, err) | ||
|
||
tool.dir = t.TempDir() | ||
tool.initialize() | ||
require.NoError(t, tool.err) | ||
|
||
tm := teatest.NewTestModel(t, tool, teatest.WithInitialTermSize(300, 100)) | ||
teatest.WaitFor(t, tm.Output(), func(bts []byte) bool { | ||
return bytes.Contains(bts, []byte("Configure ViDi endpoint URL")) | ||
}, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*3)) | ||
|
||
tm.Send(tea.KeyMsg{ | ||
Type: tea.KeyRunes, | ||
Runes: []rune("http://127.0.0.1:12345"), | ||
}) | ||
tm.Send(tea.KeyMsg{ | ||
Type: tea.KeyEnter, | ||
}) | ||
|
||
teatest.WaitFor(t, tm.Output(), func(bts []byte) bool { | ||
return bytes.Contains(bts, []byte("cannot contact ViDi endpoint")) | ||
}, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*3)) | ||
|
||
tm.Send(struct{}{}) | ||
tm.Send(tea.KeyMsg{ | ||
Type: tea.KeyEnter, | ||
}) | ||
|
||
teatest.WaitFor(t, tm.Output(), func(bts []byte) bool { | ||
return bytes.Contains(bts, []byte("Configure ViDi endpoint URL")) | ||
}, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*3)) | ||
|
||
tm.Send(tea.KeyMsg{ | ||
Type: tea.KeyCtrlC, | ||
}) | ||
} | ||
|
||
const validRemoveConfig = `{ | ||
"user_api_url": "http://localhost/api/users", | ||
"video_api_url": "localhost:443", | ||
"vidi_ca": "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUZoRENDQTJ5Z0F3SUJBZ0lVWTNSZmo0V1I3VkNwUEREZ2liR1Q0V1RySzlBd0RRWUpLb1pJaHZjTkFRRUwKQlFBd09qRUxNQWtHQTFVRUJoTUNVbFV4RFRBTEJnTlZCQW9NQkZacFJHa3hEVEFMQmdOVkJBc01CSFpwWkdreApEVEFMQmdOVkJBTU1CSFpwWkdrd0hoY05NalF3TlRFeE1qRXhOekEzV2hjTk1qVXdOVEV4TWpFeE56QTNXakE2Ck1Rc3dDUVlEVlFRR0V3SlNWVEVOTUFzR0ExVUVDZ3dFVm1sRWFURU5NQXNHQTFVRUN3d0VkbWxrYVRFTk1Bc0cKQTFVRUF3d0VkbWxrYVRDQ0FpSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnSVBBRENDQWdvQ2dnSUJBTGFOdjVPSgp6WmpYWGZ4enhaYWI3L2FJMjZkMWc4TEp1Y0R5SnpQY2JvUDdCSnh5RHFsUXAxT0RGZ2N0S2JmUWY4QlJnZG1oCjBKVFdJd0dyaWNwN2FJOEprd2lvQXZWRmZaOTNtTElCQUc1Z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||
tool.initialize() | ||
require.NoError(t, tool.err) | ||
|
||
tm := teatest.NewTestModel(t, tool, teatest.WithInitialTermSize(300, 100)) | ||
|
||
teatest.WaitFor(t, tm.Output(), func(bts []byte) bool { | ||
return bytes.Contains(bts, []byte("Configure ViDi endpoint URL")) | ||
}, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*3)) | ||
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_, errW := w.Write([]byte(validRemoveConfig)) | ||
require.NoError(t, errW) | ||
})) | ||
defer srv.Close() | ||
|
||
tm.Send(tea.KeyMsg{ | ||
Type: tea.KeyRunes, | ||
Runes: []rune(srv.URL), | ||
}) | ||
tm.Send(tea.KeyMsg{ | ||
Type: tea.KeyEnter, | ||
}) | ||
|
||
teatest.WaitFor(t, tm.Output(), func(bts []byte) bool { | ||
return bytes.Contains(bts, []byte("No locally stored users have found")) | ||
}, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*3)) | ||
} | ||
|
||
const toolStateValidActiveUser = `{ | ||
"endpoint": "%s", | ||
"users": [ | ||
{ | ||
"name": "user123", | ||
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiSm9obiBEb2UiLCJleHAiOjQwODI0Njc4Mzl9.TQy6X7dVkSRf92XjN-tRI9-fQjOOml6vcJn3Qb5iNt8" | ||
} | ||
], | ||
"current_user": 0 | ||
}` | ||
|
||
func TestTool_StartValidActiveUser(t *testing.T) { | ||
tool, err := New() | ||
require.NoError(t, err) | ||
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_, errW := w.Write([]byte(validRemoveConfig)) | ||
require.NoError(t, errW) | ||
})) | ||
defer srv.Close() | ||
|
||
tool.dir = t.TempDir() | ||
err = os.WriteFile(tool.dir+stateFile, []byte(fmt.Sprintf(toolStateValidActiveUser, srv.URL)), 0600) | ||
require.NoError(t, err) | ||
|
||
tool.initialize() | ||
require.NoError(t, tool.err) | ||
|
||
tm := teatest.NewTestModel(t, tool, teatest.WithInitialTermSize(300, 100)) | ||
|
||
teatest.WaitFor(t, tm.Output(), func(bts []byte) bool { | ||
return bytes.Contains(bts, []byte(greetMessageTxt)) | ||
}, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*3)) | ||
} | ||
|
||
const toolStateValidActiveUserExpiredToken = `{ | ||
"endpoint": "%s", | ||
"users": [ | ||
{ | ||
"name": "testUser", | ||
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJQQXg3b0RZOVJQZVJVS09FSG1GQXpnIiwibmFtZSI6InVzZXIxMjMiLCJleHAiOjE3MTU1NjI4MjV9.SdMNc0Pf5EWWf5SwPjVpy8PtLbnFT0U-XzoD7-Ayg6Y" | ||
} | ||
], | ||
"current_user": 0 | ||
}` | ||
|
||
func TestTool_StartValidActiveUserExpiredToken(t *testing.T) { | ||
tool, err := New() | ||
require.NoError(t, err) | ||
|
||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_, errW := w.Write([]byte(validRemoveConfig)) | ||
require.NoError(t, errW) | ||
})) | ||
defer srv.Close() | ||
|
||
tool.dir = t.TempDir() | ||
err = os.WriteFile(tool.dir+stateFile, []byte(fmt.Sprintf(toolStateValidActiveUserExpiredToken, srv.URL)), 0600) | ||
require.NoError(t, err) | ||
|
||
tool.initialize() | ||
require.NoError(t, tool.err) | ||
|
||
tm := teatest.NewTestModel(t, tool, teatest.WithInitialTermSize(300, 100)) | ||
|
||
teatest.WaitFor(t, tm.Output(), func(bts []byte) bool { | ||
return bytes.Contains(bts, []byte("Enter your password again or select another user")) && | ||
bytes.Contains(bts, []byte("Enter password for 'testUser'")) && | ||
bytes.Contains(bts, []byte("Login with another user")) | ||
}, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*3)) | ||
|
||
tm.Send(tea.KeyMsg{ | ||
Type: tea.KeyEnter, | ||
}) | ||
|
||
teatest.WaitFor(t, tm.Output(), func(bts []byte) bool { | ||
return bytes.Contains(bts, []byte("Enter password for 'testUser'")) && | ||
!bytes.Contains(bts, []byte("Login with another user")) | ||
}, teatest.WithCheckInterval(time.Millisecond*100), teatest.WithDuration(time.Second*3)) | ||
} |