mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-05 17:10:30 +00:00
remove repo for document tokens
This commit is contained in:
parent
d6da63187b
commit
62369f2cb0
3 changed files with 0 additions and 220 deletions
|
@ -1,68 +0,0 @@
|
||||||
package repo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
|
||||||
"github.com/hay-kot/homebox/backend/internal/data/ent/documenttoken"
|
|
||||||
)
|
|
||||||
|
|
||||||
// DocumentTokensRepository is a repository for Document entity
|
|
||||||
type DocumentTokensRepository struct {
|
|
||||||
db *ent.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
type (
|
|
||||||
DocumentToken struct {
|
|
||||||
ID uuid.UUID `json:"-"`
|
|
||||||
TokenHash []byte `json:"tokenHash"`
|
|
||||||
ExpiresAt time.Time `json:"expiresAt"`
|
|
||||||
DocumentID uuid.UUID `json:"documentId"`
|
|
||||||
}
|
|
||||||
|
|
||||||
DocumentTokenCreate struct {
|
|
||||||
TokenHash []byte `json:"tokenHash"`
|
|
||||||
DocumentID uuid.UUID `json:"documentId"`
|
|
||||||
ExpiresAt time.Time `json:"expiresAt"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
mapDocumentTokenErr = mapTErrFunc(mapDocumentToken)
|
|
||||||
)
|
|
||||||
|
|
||||||
func mapDocumentToken(e *ent.DocumentToken) DocumentToken {
|
|
||||||
return DocumentToken{
|
|
||||||
ID: e.ID,
|
|
||||||
TokenHash: e.Token,
|
|
||||||
ExpiresAt: e.ExpiresAt,
|
|
||||||
DocumentID: e.Edges.Document.ID,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *DocumentTokensRepository) Create(ctx context.Context, data DocumentTokenCreate) (DocumentToken, error) {
|
|
||||||
result, err := r.db.DocumentToken.Create().
|
|
||||||
SetDocumentID(data.DocumentID).
|
|
||||||
SetToken(data.TokenHash).
|
|
||||||
SetExpiresAt(data.ExpiresAt).
|
|
||||||
Save(ctx)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return DocumentToken{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return mapDocumentTokenErr(r.db.DocumentToken.Query().
|
|
||||||
Where(documenttoken.ID(result.ID)).
|
|
||||||
WithDocument().
|
|
||||||
Only(ctx))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *DocumentTokensRepository) PurgeExpiredTokens(ctx context.Context) (int, error) {
|
|
||||||
return r.db.DocumentToken.Delete().Where(documenttoken.ExpiresAtLT(time.Now())).Exec(ctx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *DocumentTokensRepository) Delete(ctx context.Context, id uuid.UUID) error {
|
|
||||||
return r.db.DocumentToken.DeleteOneID(id).Exec(ctx)
|
|
||||||
}
|
|
|
@ -1,150 +0,0 @@
|
||||||
package repo
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/google/uuid"
|
|
||||||
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
|
||||||
"github.com/hay-kot/homebox/backend/internal/data/ent/documenttoken"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestDocumentTokensRepository_Create(t *testing.T) {
|
|
||||||
entities := useDocs(t, 1)
|
|
||||||
doc := entities[0]
|
|
||||||
expires := fk.Time()
|
|
||||||
|
|
||||||
type args struct {
|
|
||||||
ctx context.Context
|
|
||||||
data DocumentTokenCreate
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
args args
|
|
||||||
want *ent.DocumentToken
|
|
||||||
wantErr bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "create document token",
|
|
||||||
args: args{
|
|
||||||
ctx: context.Background(),
|
|
||||||
data: DocumentTokenCreate{
|
|
||||||
DocumentID: doc.ID,
|
|
||||||
TokenHash: []byte("token"),
|
|
||||||
ExpiresAt: expires,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
want: &ent.DocumentToken{
|
|
||||||
Edges: ent.DocumentTokenEdges{
|
|
||||||
Document: &ent.Document{
|
|
||||||
ID: doc.ID,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Token: []byte("token"),
|
|
||||||
ExpiresAt: expires,
|
|
||||||
},
|
|
||||||
wantErr: false,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "create document token with empty token",
|
|
||||||
args: args{
|
|
||||||
ctx: context.Background(),
|
|
||||||
data: DocumentTokenCreate{
|
|
||||||
DocumentID: doc.ID,
|
|
||||||
TokenHash: []byte(""),
|
|
||||||
ExpiresAt: expires,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
want: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "create document token with empty document id",
|
|
||||||
args: args{
|
|
||||||
ctx: context.Background(),
|
|
||||||
data: DocumentTokenCreate{
|
|
||||||
DocumentID: uuid.Nil,
|
|
||||||
TokenHash: []byte("token"),
|
|
||||||
ExpiresAt: expires,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
want: nil,
|
|
||||||
wantErr: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
ids := make([]uuid.UUID, 0, len(tests))
|
|
||||||
|
|
||||||
t.Cleanup(func() {
|
|
||||||
for _, id := range ids {
|
|
||||||
_ = tRepos.DocTokens.Delete(context.Background(), id)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
|
|
||||||
got, err := tRepos.DocTokens.Create(tt.args.ctx, tt.args.data)
|
|
||||||
if (err != nil) != tt.wantErr {
|
|
||||||
t.Errorf("DocumentTokensRepository.Create() error = %v, wantErr %v", err, tt.wantErr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if tt.wantErr {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.Equal(t, tt.want.Token, got.TokenHash)
|
|
||||||
assert.WithinDuration(t, tt.want.ExpiresAt, got.ExpiresAt, time.Duration(1)*time.Second)
|
|
||||||
assert.Equal(t, tt.want.Edges.Document.ID, got.DocumentID)
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func useDocTokens(t *testing.T, num int) []DocumentToken {
|
|
||||||
entity := useDocs(t, 1)[0]
|
|
||||||
|
|
||||||
results := make([]DocumentToken, 0, num)
|
|
||||||
|
|
||||||
ids := make([]uuid.UUID, 0, num)
|
|
||||||
t.Cleanup(func() {
|
|
||||||
for _, id := range ids {
|
|
||||||
_ = tRepos.DocTokens.Delete(context.Background(), id)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
for i := 0; i < num; i++ {
|
|
||||||
e, err := tRepos.DocTokens.Create(context.Background(), DocumentTokenCreate{
|
|
||||||
DocumentID: entity.ID,
|
|
||||||
TokenHash: []byte(fk.Str(10)),
|
|
||||||
ExpiresAt: fk.Time(),
|
|
||||||
})
|
|
||||||
|
|
||||||
assert.NoError(t, err)
|
|
||||||
results = append(results, e)
|
|
||||||
ids = append(ids, e.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
return results
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestDocumentTokensRepository_PurgeExpiredTokens(t *testing.T) {
|
|
||||||
entities := useDocTokens(t, 2)
|
|
||||||
|
|
||||||
// set expired token
|
|
||||||
tRepos.DocTokens.db.DocumentToken.Update().
|
|
||||||
Where(documenttoken.ID(entities[0].ID)).
|
|
||||||
SetExpiresAt(time.Now().Add(-time.Hour)).
|
|
||||||
ExecX(context.Background())
|
|
||||||
|
|
||||||
count, err := tRepos.DocTokens.PurgeExpiredTokens(context.Background())
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Equal(t, 1, count)
|
|
||||||
|
|
||||||
all, err := tRepos.DocTokens.db.DocumentToken.Query().All(context.Background())
|
|
||||||
assert.NoError(t, err)
|
|
||||||
assert.Len(t, all, 1)
|
|
||||||
assert.Equal(t, entities[1].ID, all[0].ID)
|
|
||||||
}
|
|
|
@ -11,7 +11,6 @@ type AllRepos struct {
|
||||||
Labels *LabelRepository
|
Labels *LabelRepository
|
||||||
Items *ItemsRepository
|
Items *ItemsRepository
|
||||||
Docs *DocumentRepository
|
Docs *DocumentRepository
|
||||||
DocTokens *DocumentTokensRepository
|
|
||||||
Attachments *AttachmentRepo
|
Attachments *AttachmentRepo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,7 +23,6 @@ func New(db *ent.Client, root string) *AllRepos {
|
||||||
Labels: &LabelRepository{db},
|
Labels: &LabelRepository{db},
|
||||||
Items: &ItemsRepository{db},
|
Items: &ItemsRepository{db},
|
||||||
Docs: &DocumentRepository{db, root},
|
Docs: &DocumentRepository{db, root},
|
||||||
DocTokens: &DocumentTokensRepository{db},
|
|
||||||
Attachments: &AttachmentRepo{db},
|
Attachments: &AttachmentRepo{db},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue