Skip to content

Commit

Permalink
refactor: Move mocks to their respective packages
Browse files Browse the repository at this point in the history
This commit moves the mock structures and methods from the test package
to their respective packages. This change makes the codebase more
organized and easier to navigate. The MockEntryCrypto was moved to the
services package and the MockEntryModel was moved to the models package.
  • Loading branch information
Ajnasz committed Apr 3, 2024
1 parent 1b0f938 commit 9f06c13
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
25 changes: 5 additions & 20 deletions internal/test/mocks.go → internal/models/mock.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
package test
package models

import (
"context"
"database/sql"
"time"

"github.com/Ajnasz/sekret.link/internal/models"
"github.com/stretchr/testify/mock"
)

type MockEntryCrypto struct {
mock.Mock
}

func (m *MockEntryCrypto) Encrypt(data []byte) ([]byte, error) {
args := m.Called(data)
return args.Get(0).([]byte), args.Error(1)
}

func (m *MockEntryCrypto) Decrypt(data []byte) ([]byte, error) {
args := m.Called(data)
return args.Get(0).([]byte), args.Error(1)
}

type MockEntryModel struct {
mock.Mock
}
Expand All @@ -33,14 +18,14 @@ func (m *MockEntryModel) CreateEntry(
UUID string,
data []byte,
remainingReads int,
expire time.Duration) (*models.EntryMeta, error) {
expire time.Duration) (*EntryMeta, error) {
args := m.Called(ctx, tx, UUID, data, remainingReads, expire)
return args.Get(0).(*models.EntryMeta), args.Error(1)
return args.Get(0).(*EntryMeta), args.Error(1)
}

func (m *MockEntryModel) ReadEntry(ctx context.Context, tx *sql.Tx, UUID string) (*models.Entry, error) {
func (m *MockEntryModel) ReadEntry(ctx context.Context, tx *sql.Tx, UUID string) (*Entry, error) {
args := m.Called(ctx, tx, UUID)
return args.Get(0).(*models.Entry), args.Error(1)
return args.Get(0).(*Entry), args.Error(1)
}

func (m *MockEntryModel) UpdateAccessed(ctx context.Context, tx *sql.Tx, UUID string) error {
Expand Down
29 changes: 14 additions & 15 deletions internal/services/entrymanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/Ajnasz/sekret.link/internal/key"
"github.com/Ajnasz/sekret.link/internal/models"
"github.com/Ajnasz/sekret.link/internal/test"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand All @@ -32,7 +31,7 @@ func Test_EntryService_Create(t *testing.T) {

data := []byte("data")
encryptedData := []byte("encrypted")
entryModel := new(test.MockEntryModel)
entryModel := new(models.MockEntryModel)
entryModel.
On("CreateEntry", ctx, mock.Anything, mock.Anything, encryptedData, 1, mock.Anything).
Return(&models.EntryMeta{
Expand All @@ -43,7 +42,7 @@ func Test_EntryService_Create(t *testing.T) {
Expire: timenow.Add(time.Minute),
}, nil)

entryCrypto := new(test.MockEntryCrypto)
entryCrypto := new(MockEntryCrypto)
entryCrypto.On("Encrypt", data).Return(encryptedData, nil)
crypto := func(key []byte) Encrypter {
return entryCrypto
Expand Down Expand Up @@ -100,12 +99,12 @@ func TestCreateError(t *testing.T) {
data := []byte("data")
encryptedData := []byte("encrypted")

entryModel := new(test.MockEntryModel)
entryModel := new(models.MockEntryModel)
entryModel.
On("CreateEntry", ctx, mock.Anything, mock.Anything, encryptedData, 1, mock.Anything).
Return(&models.EntryMeta{}, fmt.Errorf("error"))

entryCrypto := new(test.MockEntryCrypto)
entryCrypto := new(MockEntryCrypto)
entryCrypto.On("Encrypt", data).Return(encryptedData, nil)
crypto := func(key []byte) Encrypter {
return entryCrypto
Expand Down Expand Up @@ -148,7 +147,7 @@ func TestReadEntry(t *testing.T) {
Data: []byte("encrypted"),
}

entryModel := new(test.MockEntryModel)
entryModel := new(models.MockEntryModel)
entryModel.
On("ReadEntry", ctx, mock.Anything, "uuid").
Return(&entry, nil)
Expand All @@ -157,7 +156,7 @@ func TestReadEntry(t *testing.T) {
Return(nil)

key := []byte("key")
entryCrypto := new(test.MockEntryCrypto)
entryCrypto := new(MockEntryCrypto)
entryCrypto.On("Decrypt", []byte("encrypted")).Return([]byte("data"), nil)

crypto := func(key []byte) Encrypter {
Expand Down Expand Up @@ -198,12 +197,12 @@ func TestReadEntryError(t *testing.T) {

ctx := context.Background()

entryModel := new(test.MockEntryModel)
entryModel := new(models.MockEntryModel)
entryModel.
On("ReadEntry", ctx, mock.Anything, "uuid").
Return(&models.Entry{}, fmt.Errorf("error"))

entryCrypto := new(test.MockEntryCrypto)
entryCrypto := new(MockEntryCrypto)
crypto := func(key []byte) Encrypter {
return entryCrypto
}
Expand Down Expand Up @@ -234,12 +233,12 @@ func TestDeleteEntry(t *testing.T) {

ctx := context.Background()

entryModel := new(test.MockEntryModel)
entryModel := new(models.MockEntryModel)
entryModel.
On("DeleteEntry", ctx, mock.Anything, "uuid", "delete_key").
Return(nil)

entryCrypto := new(test.MockEntryCrypto)
entryCrypto := new(MockEntryCrypto)
crypto := func(key []byte) Encrypter {
return entryCrypto
}
Expand Down Expand Up @@ -269,12 +268,12 @@ func TestDeleteEntryError(t *testing.T) {

ctx := context.Background()

entryModel := new(test.MockEntryModel)
entryModel := new(models.MockEntryModel)
entryModel.
On("DeleteEntry", ctx, mock.Anything, "uuid", "delete_key").
Return(fmt.Errorf("error"))

entryCrypto := new(test.MockEntryCrypto)
entryCrypto := new(MockEntryCrypto)
crypto := func(key []byte) Encrypter {
return entryCrypto
}
Expand Down Expand Up @@ -305,12 +304,12 @@ func TestDeleteEntryInvalidDeleteKey(t *testing.T) {

ctx := context.Background()

entryModel := new(test.MockEntryModel)
entryModel := new(models.MockEntryModel)
entryModel.
On("DeleteEntry", ctx, mock.Anything, "uuid", "delete_key").
Return(models.ErrEntryNotFound)

entryCrypto := new(test.MockEntryCrypto)
entryCrypto := new(MockEntryCrypto)
crypto := func(key []byte) Encrypter {
return entryCrypto
}
Expand Down
14 changes: 14 additions & 0 deletions internal/services/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@ func (m *MockEntryKeyer) GenerateEncryptionKey(ctx context.Context, entryUUID st
args := m.Called(ctx, entryUUID, existingKey, expire, maxRead)
return args.Get(0).(*EntryKey), args.Get(1).(*key.Key), args.Error(2)
}

type MockEntryCrypto struct {
mock.Mock
}

func (m *MockEntryCrypto) Encrypt(data []byte) ([]byte, error) {
args := m.Called(data)
return args.Get(0).([]byte), args.Error(1)
}

func (m *MockEntryCrypto) Decrypt(data []byte) ([]byte, error) {
args := m.Called(data)
return args.Get(0).([]byte), args.Error(1)
}

0 comments on commit 9f06c13

Please sign in to comment.