Skip to content

Commit 6584f87

Browse files
committed
Add more unit tests
1 parent b076f5b commit 6584f87

File tree

7 files changed

+652
-115
lines changed

7 files changed

+652
-115
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package auth
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"npm/internal/test"
8+
9+
"github.com/DATA-DOG/go-sqlmock"
10+
"github.com/stretchr/testify/assert"
11+
"github.com/stretchr/testify/require"
12+
"github.com/stretchr/testify/suite"
13+
)
14+
15+
// +------------+
16+
// | Setup |
17+
// +------------+
18+
19+
type testsuite struct {
20+
suite.Suite
21+
mock sqlmock.Sqlmock
22+
singleRow *sqlmock.Rows
23+
}
24+
25+
// SetupTest is executed before each test
26+
func (s *testsuite) SetupTest() {
27+
var err error
28+
s.mock, err = test.Setup()
29+
require.NoError(s.T(), err)
30+
31+
// These rows need to be intantiated for each test as they are
32+
// read in the db object, and their row position is not resettable
33+
// between tests.
34+
s.singleRow = sqlmock.NewRows([]string{
35+
"id",
36+
"user_id",
37+
"type",
38+
"secret",
39+
}).AddRow(
40+
10,
41+
100,
42+
TypePassword,
43+
"abc123",
44+
)
45+
}
46+
47+
// In order for 'go test' to run this suite, we need to create
48+
// a normal test function and pass our suite to suite.Run
49+
func TestExampleTestSuite(t *testing.T) {
50+
suite.Run(t, new(testsuite))
51+
}
52+
53+
func assertModel(t *testing.T, m Model) {
54+
assert.Equal(t, uint(10), m.ID)
55+
assert.Equal(t, uint(100), m.UserID)
56+
assert.Equal(t, TypePassword, m.Type)
57+
assert.Equal(t, "abc123", m.Secret)
58+
}
59+
60+
// +------------+
61+
// | Tests |
62+
// +------------+
63+
64+
func (s *testsuite) TestGetByID() {
65+
s.mock.
66+
ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "auth" WHERE "auth"."id" = $1 AND "auth"."is_deleted" = $2 ORDER BY "auth"."id" LIMIT 1`)).
67+
WithArgs(10, 0).
68+
WillReturnRows(s.singleRow)
69+
70+
m, err := GetByID(10)
71+
require.NoError(s.T(), err)
72+
require.NoError(s.T(), s.mock.ExpectationsWereMet())
73+
assertModel(s.T(), m)
74+
}
75+
76+
func (s *testsuite) TestGetByUserIDType() {
77+
s.mock.
78+
ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "auth" WHERE user_id = $1 AND type = $2 AND "auth"."is_deleted" = $3 ORDER BY "auth"."id" LIMIT 1`)).
79+
WithArgs(100, TypePassword, 0).
80+
WillReturnRows(s.singleRow)
81+
82+
m, err := GetByUserIDType(100, TypePassword)
83+
require.NoError(s.T(), err)
84+
require.NoError(s.T(), s.mock.ExpectationsWereMet())
85+
assertModel(s.T(), m)
86+
}
87+
88+
func (s *testsuite) TestSave() {
89+
s.mock.ExpectBegin()
90+
s.mock.ExpectQuery(regexp.QuoteMeta(`INSERT INTO "auth" ("created_at","updated_at","is_deleted","user_id","type","secret") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "id"`)).
91+
WithArgs(
92+
sqlmock.AnyArg(),
93+
sqlmock.AnyArg(),
94+
0,
95+
100,
96+
TypePassword,
97+
"abc123",
98+
).
99+
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("11"))
100+
s.mock.ExpectCommit()
101+
102+
// New model
103+
m := Model{
104+
UserID: 100,
105+
Type: TypePassword,
106+
Secret: "abc123",
107+
}
108+
err := m.Save()
109+
require.NoError(s.T(), err)
110+
require.NoError(s.T(), s.mock.ExpectationsWereMet())
111+
}
112+
113+
func (s *testsuite) TestSetPassword() {
114+
m := Model{UserID: 100}
115+
err := m.SetPassword("abc123")
116+
require.NoError(s.T(), err)
117+
assert.Equal(s.T(), TypePassword, m.Type)
118+
assert.Greater(s.T(), len(m.Secret), 15)
119+
120+
}
121+
122+
func (s *testsuite) TestValidateSecret() {
123+
m := Model{UserID: 100}
124+
m.SetPassword("abc123")
125+
126+
err := m.ValidateSecret("abc123")
127+
require.NoError(s.T(), err)
128+
err = m.ValidateSecret("this is not the password")
129+
assert.NotNil(s.T(), err)
130+
assert.Equal(s.T(), "Invalid Password", err.Error())
131+
132+
m.Type = "not a valid type"
133+
err = m.ValidateSecret("abc123")
134+
assert.NotNil(s.T(), err)
135+
assert.Equal(s.T(), "Could not validate Secret, auth type is not a Password", err.Error())
136+
}

backend/internal/entity/auth/model.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,6 @@ func (m *Model) LoadByID(id int) error {
3333
return result.Error
3434
}
3535

36-
// LoadByUserIDType will load from an ID
37-
func (m *Model) LoadByUserIDType(userID int, authType string) error {
38-
db := database.GetDB()
39-
result := db.
40-
Where("user_id = ?", userID).
41-
Where("type = ?", authType).
42-
First(&m)
43-
return result.Error
44-
}
45-
46-
/*
47-
// Touch will update model's timestamp(s)
48-
func (m *Model) Touch(created bool) {
49-
var d types.DBDate
50-
d.Time = time.Now()
51-
if created {
52-
m.CreatedOn = d
53-
}
54-
m.ModifiedOn = d
55-
}
56-
*/
57-
5836
// Save will save this model to the DB
5937
func (m *Model) Save() error {
6038
db := database.GetDB()
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
package certificateauthority
2+
3+
import (
4+
"regexp"
5+
"testing"
6+
7+
"npm/internal/errors"
8+
"npm/internal/model"
9+
"npm/internal/test"
10+
11+
"github.com/DATA-DOG/go-sqlmock"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
"github.com/stretchr/testify/suite"
15+
)
16+
17+
// +------------+
18+
// | Setup |
19+
// +------------+
20+
21+
type testsuite struct {
22+
suite.Suite
23+
mock sqlmock.Sqlmock
24+
testCA *sqlmock.Rows
25+
listCountRows *sqlmock.Rows
26+
listRows *sqlmock.Rows
27+
}
28+
29+
// SetupTest is executed before each test
30+
func (s *testsuite) SetupTest() {
31+
var err error
32+
s.mock, err = test.Setup()
33+
require.NoError(s.T(), err)
34+
35+
// These rows need to be intantiated for each test as they are
36+
// read in the db object, and their row position is not resettable
37+
// between tests.
38+
s.testCA = sqlmock.NewRows([]string{
39+
"id",
40+
"name",
41+
"acmesh_server",
42+
"ca_bundle",
43+
"is_wildcard_supported",
44+
"max_domains",
45+
}).AddRow(
46+
10,
47+
"Test CA",
48+
"https://ca.internal/acme/acme/directory",
49+
"/etc/ssl/certs/NginxProxyManager.crt",
50+
true,
51+
2,
52+
)
53+
54+
s.listCountRows = sqlmock.NewRows([]string{
55+
"count(*)",
56+
}).AddRow(
57+
2,
58+
)
59+
60+
s.listRows = sqlmock.NewRows([]string{
61+
"id",
62+
"name",
63+
"acmesh_server",
64+
"ca_bundle",
65+
"is_wildcard_supported",
66+
"max_domains",
67+
}).AddRow(
68+
10,
69+
"Test CA",
70+
"https://ca.internal/acme/acme/directory",
71+
"/etc/ssl/certs/NginxProxyManager.crt",
72+
true,
73+
2,
74+
).AddRow(
75+
11,
76+
"Test CA 2",
77+
"https://ca2.internal/acme/acme/directory",
78+
"/etc/ssl/certs/NginxProxyManager.crt",
79+
true,
80+
5,
81+
)
82+
}
83+
84+
// In order for 'go test' to run this suite, we need to create
85+
// a normal test function and pass our suite to suite.Run
86+
func TestExampleTestSuite(t *testing.T) {
87+
suite.Run(t, new(testsuite))
88+
}
89+
90+
func assertModel(t *testing.T, m Model) {
91+
assert.Equal(t, uint(10), m.ID)
92+
assert.Equal(t, "Test CA", m.Name)
93+
assert.Equal(t, "https://ca.internal/acme/acme/directory", m.AcmeshServer)
94+
assert.Equal(t, "/etc/ssl/certs/NginxProxyManager.crt", m.CABundle)
95+
assert.Equal(t, 2, m.MaxDomains)
96+
assert.Equal(t, true, m.IsWildcardSupported)
97+
assert.Equal(t, false, m.IsReadonly)
98+
}
99+
100+
// +------------+
101+
// | Tests |
102+
// +------------+
103+
104+
func (s *testsuite) TestGetByID() {
105+
s.mock.
106+
ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "certificate_authority" WHERE "certificate_authority"."id" = $1 AND "certificate_authority"."is_deleted" = $2 ORDER BY "certificate_authority"."id" LIMIT 1`)).
107+
WithArgs(10, 0).
108+
WillReturnRows(s.testCA)
109+
110+
m, err := GetByID(10)
111+
require.NoError(s.T(), err)
112+
require.NoError(s.T(), s.mock.ExpectationsWereMet())
113+
assertModel(s.T(), m)
114+
}
115+
116+
func (s *testsuite) TestList() {
117+
s.mock.
118+
ExpectQuery(regexp.QuoteMeta(`SELECT count(*) FROM "certificate_authority" WHERE name LIKE $1 AND "certificate_authority"."is_deleted" = $2`)).
119+
WithArgs("%test%", 0).
120+
WillReturnRows(s.listCountRows)
121+
122+
s.mock.
123+
ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "certificate_authority" WHERE name LIKE $1 AND "certificate_authority"."is_deleted" = $2 ORDER BY name asc LIMIT 8`)).
124+
WithArgs("%test%", 0).
125+
WillReturnRows(s.listRows)
126+
127+
p := model.PageInfo{
128+
Offset: 0,
129+
Limit: 8,
130+
Sort: []model.Sort{
131+
{
132+
Field: "name",
133+
Direction: "asc",
134+
},
135+
},
136+
}
137+
138+
f := []model.Filter{
139+
{
140+
Field: "name",
141+
Modifier: "contains",
142+
Value: []string{"test"},
143+
},
144+
}
145+
146+
resp, err := List(p, f)
147+
require.NoError(s.T(), err)
148+
assert.Equal(s.T(), int64(2), resp.Total)
149+
assert.Equal(s.T(), p.Offset, resp.Offset)
150+
assert.Equal(s.T(), p.Limit, resp.Limit)
151+
assert.Equal(s.T(), p.Limit, resp.Limit)
152+
assert.Equal(s.T(), p.Sort, resp.Sort)
153+
assert.Equal(s.T(), f, resp.Filter)
154+
155+
require.NoError(s.T(), s.mock.ExpectationsWereMet())
156+
}
157+
158+
func (s *testsuite) TestSave() {
159+
s.mock.ExpectBegin()
160+
s.mock.ExpectQuery(regexp.QuoteMeta(`INSERT INTO "certificate_authority" ("created_at","updated_at","is_deleted","name","acmesh_server","ca_bundle","max_domains","is_wildcard_supported","is_readonly") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9) RETURNING "id"`)).
161+
WithArgs(
162+
sqlmock.AnyArg(),
163+
sqlmock.AnyArg(),
164+
0,
165+
"Test CA",
166+
"https://ca.internal/acme/acme/directory",
167+
"/etc/ssl/certs/NginxProxyManager.crt",
168+
2,
169+
true,
170+
false,
171+
).
172+
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("11"))
173+
s.mock.ExpectCommit()
174+
175+
m := Model{
176+
Name: "Test CA",
177+
AcmeshServer: "https://ca.internal/acme/acme/directory",
178+
CABundle: "/etc/ssl/certs/NginxProxyManager.crt",
179+
MaxDomains: 2,
180+
IsWildcardSupported: true,
181+
}
182+
err := m.Save()
183+
require.NoError(s.T(), err)
184+
require.NoError(s.T(), s.mock.ExpectationsWereMet())
185+
}
186+
187+
func (s *testsuite) TestDelete() {
188+
s.mock.ExpectBegin()
189+
s.mock.
190+
ExpectExec(regexp.QuoteMeta(`UPDATE "certificate_authority" SET "is_deleted"=$1 WHERE "certificate_authority"."id" = $2 AND "certificate_authority"."is_deleted" = $3`)).
191+
WithArgs(1, 10, 0).
192+
WillReturnResult(sqlmock.NewResult(0, 1))
193+
s.mock.ExpectCommit()
194+
195+
m := Model{}
196+
err := m.Delete()
197+
assert.Equal(s.T(), "Unable to delete a new object", err.Error())
198+
199+
m2 := Model{
200+
ModelBase: model.ModelBase{
201+
ID: 10,
202+
},
203+
}
204+
err2 := m2.Delete()
205+
require.NoError(s.T(), err2)
206+
require.NoError(s.T(), s.mock.ExpectationsWereMet())
207+
}
208+
209+
func (s *testsuite) TestCheck() {
210+
m := Model{}
211+
err := m.Check()
212+
assert.Nil(s.T(), err)
213+
214+
m.CABundle = "/tmp/doesnotexist"
215+
err = m.Check()
216+
assert.Equal(s.T(), errors.ErrCABundleDoesNotExist.Error(), err.Error())
217+
}

0 commit comments

Comments
 (0)