Skip to content

Commit

Permalink
Add deactivateThreshold in project
Browse files Browse the repository at this point in the history
  • Loading branch information
krapie committed Jan 27, 2023
1 parent c57cbd6 commit 317535a
Show file tree
Hide file tree
Showing 23 changed files with 473 additions and 307 deletions.
21 changes: 13 additions & 8 deletions api/converter/from_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package converter

import (
"fmt"
gotime "time"

protoTypes "github.com/gogo/protobuf/types"

Expand Down Expand Up @@ -69,14 +70,15 @@ func FromProject(pbProject *api.Project) (*types.Project, error) {
return nil, fmt.Errorf("convert updatedAt to timestamp: %w", err)
}
return &types.Project{
ID: types.ID(pbProject.Id),
Name: pbProject.Name,
AuthWebhookURL: pbProject.AuthWebhookUrl,
AuthWebhookMethods: pbProject.AuthWebhookMethods,
PublicKey: pbProject.PublicKey,
SecretKey: pbProject.SecretKey,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
ID: types.ID(pbProject.Id),
Name: pbProject.Name,
AuthWebhookURL: pbProject.AuthWebhookUrl,
AuthWebhookMethods: pbProject.AuthWebhookMethods,
DeactivateThreshold: gotime.Duration(pbProject.DeactivateThreshold) * gotime.Millisecond,
PublicKey: pbProject.PublicKey,
SecretKey: pbProject.SecretKey,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}, nil
}

Expand Down Expand Up @@ -663,6 +665,9 @@ func FromUpdatableProjectFields(pbProjectFields *api.UpdatableProjectFields) (*t
if pbProjectFields.AuthWebhookMethods != nil {
updatableProjectFields.AuthWebhookMethods = &pbProjectFields.AuthWebhookMethods.Methods
}
if pbProjectFields.DeactivateThreshold != 0 {
updatableProjectFields.DeactivateThreshold = pbProjectFields.DeactivateThreshold
}

return updatableProjectFields, nil
}
20 changes: 12 additions & 8 deletions api/converter/to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ func ToProject(project *types.Project) (*api.Project, error) {
}

return &api.Project{
Id: project.ID.String(),
Name: project.Name,
AuthWebhookUrl: project.AuthWebhookURL,
AuthWebhookMethods: project.AuthWebhookMethods,
PublicKey: project.PublicKey,
SecretKey: project.SecretKey,
CreatedAt: pbCreatedAt,
UpdatedAt: pbUpdatedAt,
Id: project.ID.String(),
Name: project.Name,
AuthWebhookUrl: project.AuthWebhookURL,
AuthWebhookMethods: project.AuthWebhookMethods,
DeactivateThreshold: project.DeactivateThreshold.Milliseconds(),
PublicKey: project.PublicKey,
SecretKey: project.SecretKey,
CreatedAt: pbCreatedAt,
UpdatedAt: pbUpdatedAt,
}, nil
}

Expand Down Expand Up @@ -511,6 +512,9 @@ func ToUpdatableProjectFields(fields *types.UpdatableProjectFields) (*api.Updata
pbUpdatableProjectFields.AuthWebhookMethods = &api.UpdatableProjectFields_AuthWebhookMethods{
Methods: *fields.AuthWebhookMethods,
}
}
if fields.DeactivateThreshold != 0 {
pbUpdatableProjectFields.DeactivateThreshold = fields.DeactivateThreshold
} else {
pbUpdatableProjectFields.AuthWebhookMethods = nil
}
Expand Down
4 changes: 4 additions & 0 deletions api/types/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type Project struct {

// UpdatedAt is the time when the project was updated.
UpdatedAt time.Time `json:"updated_at"`

// DeactivateThreshold is the time after which clients are
// considered deactivate in specific project.
DeactivateThreshold time.Duration `bson:"deactivate_threshold"`
}

// RequireAuth returns whether the given method requires authorization.
Expand Down
5 changes: 4 additions & 1 deletion api/types/updatable_project_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@ type UpdatableProjectFields struct {

// AuthWebhookMethods is the methods that run the authorization webhook.
AuthWebhookMethods *[]string `bson:"auth_webhook_methods,omitempty" validate:"omitempty,invalid_webhook_method"`

// DeactivateThreshold is the time after which clients are considered deactivate in specific project.
DeactivateThreshold int64 `bson:"deactivate_threshold,omitempty" validate:"omitempty"`
}

// Validate validates the UpdatableProjectFields.
func (i *UpdatableProjectFields) Validate() error {
if i.Name == nil && i.AuthWebhookURL == nil && i.AuthWebhookMethods == nil {
if i.Name == nil && i.AuthWebhookURL == nil && i.AuthWebhookMethods == nil && i.DeactivateThreshold == 0 {
return ErrEmptyProjectFields
}

Expand Down
21 changes: 13 additions & 8 deletions api/types/updatable_project_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package types_test

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand All @@ -33,10 +34,12 @@ func TestUpdatableProjectFields(t *testing.T) {
string(types.AttachDocument),
string(types.WatchDocuments),
}
newDeactivateThresholds := 1 * time.Hour
fields := &types.UpdatableProjectFields{
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
AuthWebhookMethods: &newAuthWebhookMethods,
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
AuthWebhookMethods: &newAuthWebhookMethods,
DeactivateThreshold: int64(newDeactivateThresholds.Milliseconds()),
}
assert.NoError(t, fields.Validate())

Expand All @@ -49,8 +52,9 @@ func TestUpdatableProjectFields(t *testing.T) {
assert.NoError(t, fields.Validate())

fields = &types.UpdatableProjectFields{
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
DeactivateThreshold: int64(newDeactivateThresholds.Milliseconds()),
}
assert.NoError(t, fields.Validate())

Expand All @@ -59,9 +63,10 @@ func TestUpdatableProjectFields(t *testing.T) {
"InvalidMethods",
}
fields = &types.UpdatableProjectFields{
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
AuthWebhookMethods: &newAuthWebhookMethods,
Name: &newName,
AuthWebhookURL: &newAuthWebhookURL,
AuthWebhookMethods: &newAuthWebhookMethods,
DeactivateThreshold: int64(newDeactivateThresholds.Milliseconds()),
}
assert.ErrorAs(t, fields.Validate(), &invalidFieldsError)
})
Expand Down
Loading

0 comments on commit 317535a

Please sign in to comment.