1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package handler
import (
"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 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, 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())
}
|