diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-08-19 00:11:32 +0200 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-08-19 00:14:38 +0200 |
commit | 1aafb164f51992fad314e612afaff8b1b0367447 (patch) | |
tree | 9b1270a0fc18baf66f14da05b7d7c777cc0c7594 /src/rest-api/handler/handler_test.go | |
parent | Migrations: Add new restored_file table migration (diff) | |
download | fs-tracer-backend-1aafb164f51992fad314e612afaff8b1b0367447.tar.gz fs-tracer-backend-1aafb164f51992fad314e612afaff8b1b0367447.tar.bz2 fs-tracer-backend-1aafb164f51992fad314e612afaff8b1b0367447.zip |
rest-api: Add /api/v1/restored-files/ endpoint
Diffstat (limited to 'src/rest-api/handler/handler_test.go')
-rw-r--r-- | src/rest-api/handler/handler_test.go | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/src/rest-api/handler/handler_test.go b/src/rest-api/handler/handler_test.go index 183d584..e3dbfe0 100644 --- a/src/rest-api/handler/handler_test.go +++ b/src/rest-api/handler/handler_test.go @@ -1,31 +1,73 @@ package handler import ( - "fmt" "net/http" "net/http/httptest" + "strings" "testing" + "time" "github.com/Baitinq/fs-tracer-backend/lib" "github.com/stretchr/testify/require" gomock "go.uber.org/mock/gomock" ) -func TestHandleGet(t *testing.T) { +func TestHandleGetFile(t *testing.T) { ctrl := gomock.NewController(t) db := NewMockDB(ctrl) recorder := httptest.NewRecorder() handler := Handler{db: db} + now := time.Now() file := &lib.File{ + Id: "ID", User_id: "USER_ID", Absolute_path: "/tmp/file.txt", + Timestamp: now, + Contents: "contents", } db.EXPECT().GetLatestFileByPath(gomock.Any(), "/tmp/file.txt", "USER_ID").Return(file, nil) handler.handleGet(recorder, httptest.NewRequest(http.MethodGet, "/file/?path=%2ftmp%2Ffile.txt", nil), "USER_ID") require.Equal(t, http.StatusOK, recorder.Code) - require.Equal(t, fmt.Sprintln("File: ", file), recorder.Body.String()) + require.Equal(t, strings.Join(strings.Fields(` + { + "Id": "ID", + "User_id": "USER_ID", + "Absolute_path": "/tmp/file.txt", + "Contents": "contents", + "Timestamp": "`+now.Format(time.RFC3339Nano)+`" + }`), ""), recorder.Body.String()) +} + +func TestHandleGetRestoredFiles(t *testing.T) { + ctrl := gomock.NewController(t) + db := NewMockDB(ctrl) + recorder := httptest.NewRecorder() + + handler := Handler{db: db} + + now := time.Now() + file := &lib.File{ + Id: "ID", + User_id: "USER_ID", + Absolute_path: "/tmp/file.txt", + Timestamp: now, + Contents: "contents", + } + db.EXPECT().GetAndDeleteRestoredFiles(gomock.Any(), "USER_ID").Return(&[]lib.File{*file}, nil) + + handler.handleGet(recorder, httptest.NewRequest(http.MethodGet, "/restored-files/", nil), "USER_ID") + + require.Equal(t, http.StatusOK, recorder.Code) + require.Equal(t, strings.Join(strings.Fields(`[ + { + "Id": "ID", + "User_id": "USER_ID", + "Absolute_path": "/tmp/file.txt", + "Contents": "contents", + "Timestamp": "`+now.Format(time.RFC3339Nano)+`" + }]`), ""), recorder.Body.String()) } |