This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermissions_handler_test.go
72 lines (68 loc) · 2.31 KB
/
permissions_handler_test.go
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
package starter_test
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/fossapps/starter"
"github.com/fossapps/starter/db"
"github.com/fossapps/starter/mock"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)
func TestServer_ListPermissionsListsPermissionFromDb(t *testing.T) {
expect := assert.New(t)
permissionCtrl := gomock.NewController(t)
defer permissionCtrl.Finish()
permissionManager := mock.NewMockPermissionManager(permissionCtrl)
dbCtrl := gomock.NewController(t)
defer dbCtrl.Finish()
dbManager := mock.NewMockDB(dbCtrl)
dbManager.EXPECT().Clone().AnyTimes().Return(dbManager)
dbManager.EXPECT().Close().Times(1)
server := starter.Server{
Db: dbManager,
}
mockPermissionList := []db.Permission{
{
Description: "test description",
Key: "key",
},
{
Description: "second description",
Key: "second.key",
},
}
permissionManager.EXPECT().List().Times(1).Return(mockPermissionList, nil)
dbManager.EXPECT().Permissions().Times(1).Return(permissionManager)
responseRecorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", nil)
server.ListPermissions()(responseRecorder, request)
response := new([]db.Permission)
json.NewDecoder(responseRecorder.Body).Decode(&response)
expect.Equal(http.StatusOK, responseRecorder.Code)
expect.Equal(response, &mockPermissionList)
}
func TestServer_ListPermissionsReturnsInternalServerErrorIfDbError(t *testing.T) {
expect := assert.New(t)
permissionCtrl := gomock.NewController(t)
defer permissionCtrl.Finish()
permissionManager := mock.NewMockPermissionManager(permissionCtrl)
dbCtrl := gomock.NewController(t)
defer dbCtrl.Finish()
dbManager := mock.NewMockDB(dbCtrl)
dbManager.EXPECT().Clone().AnyTimes().Return(dbManager)
dbManager.EXPECT().Close().Times(1)
server := starter.Server{
Db: dbManager,
}
permissionManager.EXPECT().List().Times(1).Return(nil, errors.New("db error"))
dbManager.EXPECT().Permissions().Times(1).Return(permissionManager)
responseRecorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", nil)
server.ListPermissions()(responseRecorder, request)
response := new([]db.Permission)
json.NewDecoder(responseRecorder.Body).Decode(&response)
expect.Equal(http.StatusInternalServerError, responseRecorder.Code)
}