11package legacy
22
33import (
4+ "bytes"
5+ "io"
6+ "net/http/httptest"
7+ "strings"
48 "testing"
59
610 "connectrpc.com/connect"
711 "github.com/rs/zerolog"
812 "github.com/stretchr/testify/assert"
13+ "github.com/stretchr/testify/mock"
14+ "github.com/stretchr/testify/require"
915
1016 "github.com/e2b-dev/infra/packages/envd/internal/services/spec/filesystem"
17+ "github.com/e2b-dev/infra/packages/envd/internal/services/spec/filesystem/filesystemconnect"
1118)
1219
20+ func TestFilesystemClient_FieldFormatter (t * testing.T ) {
21+ fsh := NewMockFilesystemHandler (t )
22+ fsh .EXPECT ().Move (mock .Anything , mock .Anything ).Return (connect .NewResponse (& filesystem.MoveResponse {
23+ Entry : & filesystem.EntryInfo {
24+ Name : "test-name" ,
25+ Owner : "new-extra-field" ,
26+ },
27+ }), nil )
28+
29+ _ , handler := filesystemconnect .NewFilesystemHandler (fsh ,
30+ connect .WithInterceptors (
31+ Convert (),
32+ ),
33+ )
34+
35+ t .Run ("can return all fields" , func (t * testing.T ) {
36+ buf := bytes .NewBuffer ([]byte (`{}` ))
37+ req := httptest .NewRequest ("POST" , filesystemconnect .FilesystemMoveProcedure , buf )
38+ req .Header .Set ("content-type" , "application/json" )
39+ w := httptest .NewRecorder ()
40+ handler .ServeHTTP (w , req )
41+
42+ assert .Equal (t , 200 , w .Code )
43+
44+ data , err := io .ReadAll (w .Body )
45+ require .NoError (t , err )
46+
47+ // Depending on the test execution order, different json serialization settings will be used,
48+ // specifically in regard to whitespace after colons. This normalizes it so the order no
49+ // longer matters.
50+ text := strings .ReplaceAll (string (data ), " " , "" )
51+ assert .Equal (t , `{"entry":{"name":"test-name","owner":"new-extra-field"}}` , text )
52+ })
53+
54+ t .Run ("can hide fields when appropriate" , func (t * testing.T ) {
55+ buf := bytes .NewBuffer ([]byte (`{}` ))
56+ req := httptest .NewRequest ("POST" , filesystemconnect .FilesystemMoveProcedure , buf )
57+ req .Header .Set ("user-agent" , brokenUserAgent )
58+ req .Header .Set ("content-type" , "application/json" )
59+ w := httptest .NewRecorder ()
60+ handler .ServeHTTP (w , req )
61+
62+ assert .Equal (t , 200 , w .Code )
63+
64+ data , err := io .ReadAll (w .Body )
65+ require .NoError (t , err )
66+ assert .Equal (t , string (data ), `{"entry":{"name":"test-name"}}` )
67+ })
68+ }
69+
1370func TestConversion (t * testing.T ) {
1471 testCases := []struct {
1572 name string
@@ -20,9 +77,12 @@ func TestConversion(t *testing.T) {
2077 name : "MoveResponse with populated fields" ,
2178 input : connect .NewResponse (& filesystem.MoveResponse {
2279 Entry : & filesystem.EntryInfo {
23- Name : "test.txt" ,
24- Type : filesystem .FileType_FILE_TYPE_FILE ,
25- Path : "/test/test.txt" ,
80+ Name : "test.txt" ,
81+ Type : filesystem .FileType_FILE_TYPE_FILE ,
82+ Path : "/test/test.txt" ,
83+ Owner : "root" ,
84+ Group : "root" ,
85+ Size : 1024 ,
2686 },
2787 }),
2888 expected : connect .NewResponse (& MoveResponse {
@@ -43,14 +103,20 @@ func TestConversion(t *testing.T) {
43103 input : connect .NewResponse (& filesystem.ListDirResponse {
44104 Entries : []* filesystem.EntryInfo {
45105 {
46- Name : "test1.txt" ,
47- Type : filesystem .FileType_FILE_TYPE_FILE ,
48- Path : "/test/test1.txt" ,
106+ Name : "test1.txt" ,
107+ Type : filesystem .FileType_FILE_TYPE_FILE ,
108+ Path : "/test/test1.txt" ,
109+ Owner : "root" ,
110+ Group : "root" ,
111+ Size : 1024 ,
49112 },
50113 {
51- Name : "test2.txt" ,
52- Type : filesystem .FileType_FILE_TYPE_FILE ,
53- Path : "/test/test2.txt" ,
114+ Name : "test2.txt" ,
115+ Type : filesystem .FileType_FILE_TYPE_FILE ,
116+ Path : "/test/test2.txt" ,
117+ Owner : "root" ,
118+ Group : "root" ,
119+ Size : 1024 ,
54120 },
55121 },
56122 }),
@@ -78,9 +144,12 @@ func TestConversion(t *testing.T) {
78144 name : "MakeDirResponse with populated fields" ,
79145 input : connect .NewResponse (& filesystem.MakeDirResponse {
80146 Entry : & filesystem.EntryInfo {
81- Name : "testdir" ,
82- Type : filesystem .FileType_FILE_TYPE_DIRECTORY ,
83- Path : "/test/testdir" ,
147+ Name : "testdir" ,
148+ Type : filesystem .FileType_FILE_TYPE_DIRECTORY ,
149+ Path : "/test/testdir" ,
150+ Owner : "root" ,
151+ Group : "root" ,
152+ Size : 1024 ,
84153 },
85154 }),
86155 expected : connect .NewResponse (& MakeDirResponse {
@@ -105,9 +174,12 @@ func TestConversion(t *testing.T) {
105174 name : "StatResponse with populated fields" ,
106175 input : connect .NewResponse (& filesystem.StatResponse {
107176 Entry : & filesystem.EntryInfo {
108- Name : "test.txt" ,
109- Type : filesystem .FileType_FILE_TYPE_FILE ,
110- Path : "/test/test.txt" ,
177+ Name : "test.txt" ,
178+ Type : filesystem .FileType_FILE_TYPE_FILE ,
179+ Path : "/test/test.txt" ,
180+ Owner : "root" ,
181+ Group : "root" ,
182+ Size : 1024 ,
111183 },
112184 }),
113185 expected : connect .NewResponse (& StatResponse {
@@ -213,9 +285,12 @@ func TestConvertValue(t *testing.T) {
213285 "move response without value" : {
214286 input : & filesystem.MoveResponse {
215287 Entry : & filesystem.EntryInfo {
216- Name : "test.txt" ,
217- Type : filesystem .FileType_FILE_TYPE_FILE ,
218- Path : "/test/test.txt" ,
288+ Name : "test.txt" ,
289+ Type : filesystem .FileType_FILE_TYPE_FILE ,
290+ Path : "/test/test.txt" ,
291+ Owner : "root" ,
292+ Group : "root" ,
293+ Size : 1024 ,
219294 },
220295 },
221296 expected : & MoveResponse {
0 commit comments