1 package database
2
3 import (
4 "context"
5 "fmt"
6 "testing"
7
8 "github.com/DATA-DOG/go-sqlmock"
9 "github.com/stretchr/testify/assert"
10
11 "edge-infra.dev/pkg/sds/emergencyaccess/eaconst"
12 rulesengine "edge-infra.dev/pkg/sds/emergencyaccess/rules"
13 datasql "edge-infra.dev/pkg/sds/emergencyaccess/rules/storage/database/sql"
14 )
15
16 const bannerID = "2f9f5965-ed2a-4262-9fd9-9d2d8f8bee8a"
17
18 func TestEARoles(t *testing.T) {
19 t.Parallel()
20 tests := map[string]struct {
21 bannerID string
22 command rulesengine.Command
23 expRes []string
24
25 expectations func(mock sqlmock.Sqlmock)
26 errorAssertion assert.ErrorAssertionFunc
27 }{
28 "Success": {
29 bannerID: bannerID,
30 command: rulesengine.Command{
31 Name: "ls",
32 Type: eaconst.Command,
33 },
34 expectations: func(mock sqlmock.Sqlmock) {
35 mockRow := sqlmock.NewRows([]string{"privilegeName"}).AddRow("ea-read")
36 mock.ExpectQuery(datasql.SelectPrivNamesForCommandAndBanner).
37 WithArgs("ls", "command", bannerID).WillReturnRows(mockRow)
38 },
39 expRes: []string{"ea-read"},
40 errorAssertion: assert.NoError,
41 },
42 "Success Executable": {
43 bannerID: bannerID,
44 command: rulesengine.Command{
45 Name: "myScript",
46 Type: eaconst.Executable,
47 },
48 expectations: func(mock sqlmock.Sqlmock) {
49 mockRow := sqlmock.NewRows([]string{"privilegeName"}).AddRow("ea-read")
50 mock.ExpectQuery(datasql.SelectPrivNamesForCommandAndBanner).
51 WithArgs("myScript", "executable", bannerID).WillReturnRows(mockRow)
52 },
53 expRes: []string{"ea-read"},
54 errorAssertion: assert.NoError,
55 },
56 "Unknown Type": {
57 bannerID: bannerID,
58 command: rulesengine.Command{
59 Name: "myScript",
60 Type: eaconst.RequestType("invalidType"),
61 },
62 expectations: func(mock sqlmock.Sqlmock) {
63 mock.ExpectQuery(datasql.SelectPrivNamesForCommandAndBanner).
64 WithArgs("myScript", "invalidType", bannerID).
65 WillReturnError(fmt.Errorf(`Error: invalid input value for enum command_type: "Type" (SQLSTATE 22P02)`))
66 },
67 expRes: nil,
68 errorAssertion: func(tt assert.TestingT, err error, _ ...interface{}) bool {
69 return assert.EqualError(tt, err, `Error: invalid input value for enum command_type: "Type" (SQLSTATE 22P02)`)
70 },
71 },
72 "Fail no banner": {
73 bannerID: "",
74 command: rulesengine.Command{
75 Name: "ls",
76 Type: eaconst.Command,
77 },
78 expectations: func(mock sqlmock.Sqlmock) {
79 mock.ExpectQuery(datasql.SelectPrivNamesForCommandAndBanner).
80 WithArgs("ls", "command", "").WillReturnError(fmt.Errorf("uuid error"))
81 },
82 expRes: []string(nil),
83 errorAssertion: func(tt assert.TestingT, err error, _ ...interface{}) bool {
84 return assert.EqualError(tt, err, "uuid error")
85 },
86 },
87 }
88
89 for name, tc := range tests {
90 tc := tc
91 t.Run(name, func(t *testing.T) {
92 t.Parallel()
93
94 db, mock := initMockDB(t)
95 defer db.Close()
96
97 ds := Dataset{db: db}
98
99 tc.expectations(mock)
100
101 res, err := ds.EARoles(context.Background(), tc.bannerID, tc.command)
102 tc.errorAssertion(t, err)
103 assert.Equal(t, tc.expRes, res)
104
105 assert.NoError(t, mock.ExpectationsWereMet())
106 })
107 }
108 }
109
View as plain text