forked from mirrors/homebox
Initial commit
This commit is contained in:
commit
29f583e936
135 changed files with 18463 additions and 0 deletions
11
backend/internal/types/about_types.go
Normal file
11
backend/internal/types/about_types.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package types
|
||||
|
||||
// ApiSummary
|
||||
//
|
||||
// @public
|
||||
type ApiSummary struct {
|
||||
Healthy bool `json:"health"`
|
||||
Versions []string `json:"versions"`
|
||||
Title string `json:"title"`
|
||||
Message string `json:"message"`
|
||||
}
|
39
backend/internal/types/token_types.go
Normal file
39
backend/internal/types/token_types.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type LoginForm struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type TokenResponse struct {
|
||||
BearerToken string `json:"token"`
|
||||
ExpiresAt time.Time `json:"expiresAt"`
|
||||
}
|
||||
|
||||
type UserAuthTokenDetail struct {
|
||||
Raw string `json:"raw"`
|
||||
ExpiresAt time.Time `json:"expiresAt"`
|
||||
}
|
||||
|
||||
type UserAuthToken struct {
|
||||
TokenHash []byte `json:"token"`
|
||||
UserID uuid.UUID `json:"userId"`
|
||||
ExpiresAt time.Time `json:"expiresAt"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
}
|
||||
|
||||
func (u UserAuthToken) IsExpired() bool {
|
||||
return u.ExpiresAt.Before(time.Now())
|
||||
}
|
||||
|
||||
type UserAuthTokenCreate struct {
|
||||
TokenHash []byte `json:"token"`
|
||||
UserID uuid.UUID `json:"userId"`
|
||||
ExpiresAt time.Time `json:"expiresAt"`
|
||||
}
|
58
backend/internal/types/users_types.go
Normal file
58
backend/internal/types/users_types.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNameEmpty = errors.New("name is empty")
|
||||
ErrEmailEmpty = errors.New("email is empty")
|
||||
)
|
||||
|
||||
// UserIn is a basic user input struct containing only the fields that are
|
||||
// required for user creation.
|
||||
type UserIn struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
// UserCreate is the Data object contain the requirements of creating a user
|
||||
// in the database. It should to create users from an API unless the user has
|
||||
// rights to create SuperUsers. For regular user in data use the UserIn struct.
|
||||
type UserCreate struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
IsSuperuser bool `json:"isSuperuser"`
|
||||
}
|
||||
|
||||
func (u *UserCreate) Validate() error {
|
||||
if u.Name == "" {
|
||||
return ErrNameEmpty
|
||||
}
|
||||
if u.Email == "" {
|
||||
return ErrEmailEmpty
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type UserOut struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"-"`
|
||||
IsSuperuser bool `json:"isSuperuser"`
|
||||
}
|
||||
|
||||
// IsNull is a proxy call for `usr.Id == uuid.Nil`
|
||||
func (usr *UserOut) IsNull() bool {
|
||||
return usr.ID == uuid.Nil
|
||||
}
|
||||
|
||||
type UserUpdate struct {
|
||||
Name *string `json:"name"`
|
||||
Email *string `json:"email"`
|
||||
}
|
76
backend/internal/types/users_types_test.go
Normal file
76
backend/internal/types/users_types_test.go
Normal file
|
@ -0,0 +1,76 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestUserCreate_Validate(t *testing.T) {
|
||||
type fields struct {
|
||||
Name string
|
||||
Email string
|
||||
Password string
|
||||
IsSuperuser bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "no_name",
|
||||
fields: fields{
|
||||
Name: "",
|
||||
Email: "",
|
||||
Password: "",
|
||||
IsSuperuser: false,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "no_email",
|
||||
fields: fields{
|
||||
Name: "test",
|
||||
Email: "",
|
||||
Password: "",
|
||||
IsSuperuser: false,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "valid",
|
||||
fields: fields{
|
||||
Name: "test",
|
||||
Email: "test@email.com",
|
||||
Password: "mypassword",
|
||||
IsSuperuser: false,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
u := &UserCreate{
|
||||
Name: tt.fields.Name,
|
||||
Email: tt.fields.Email,
|
||||
Password: tt.fields.Password,
|
||||
IsSuperuser: tt.fields.IsSuperuser,
|
||||
}
|
||||
if err := u.Validate(); (err != nil) != tt.wantErr {
|
||||
t.Errorf("UserCreate.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserOut_IsNull(t *testing.T) {
|
||||
nullUser := UserOut{}
|
||||
|
||||
assert.True(t, nullUser.IsNull())
|
||||
|
||||
nullUser.ID = uuid.New()
|
||||
|
||||
assert.False(t, nullUser.IsNull())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue