Skip to content

Commit

Permalink
delete endpoints accepts subject_id
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkowalczuk committed Mar 19, 2017
1 parent 8452a72 commit 883966e
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 65 deletions.
2 changes: 1 addition & 1 deletion mnemosyned/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (h *handler) delete(ctx context.Context, req *mnemosynerpc.DeleteRequest) (
h.logger = log.NewContext(h.logger).With("expire_at_to", eat)
}

affected, err := h.storage.Delete(ctx, req.AccessToken, req.RefreshToken, expireAtFrom, expireAtTo)
affected, err := h.storage.Delete(ctx, req.SubjectId, req.AccessToken, req.RefreshToken, expireAtFrom, expireAtTo)
if err != nil {
return 0, err
}
Expand Down
14 changes: 7 additions & 7 deletions mnemosyned/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,20 @@ func (_m *mockStorage) Exists(_a0 context.Context, _a1 string) (bool, error) {
return r0, r1
}

// Delete provides a mock function with given fields: _a0, _a1, _a2, _a3, _a4
func (_m *mockStorage) Delete(_a0 context.Context, _a1 string, _a2 string, _a3 *time.Time, _a4 *time.Time) (int64, error) {
ret := _m.Called(_a0, _a1, _a2, _a3, _a4)
// Delete provides a mock function with given fields: _a0, _a1, _a2, _a3, _a4, _a5
func (_m *mockStorage) Delete(_a0 context.Context, _a1 string, _a2 string, _a3 string, _a4 *time.Time, _a5 *time.Time) (int64, error) {
ret := _m.Called(_a0, _a1, _a2, _a3, _a4, _a5)

var r0 int64
if rf, ok := ret.Get(0).(func(context.Context, string, string, *time.Time, *time.Time) int64); ok {
r0 = rf(_a0, _a1, _a2, _a3, _a4)
if rf, ok := ret.Get(0).(func(context.Context, string, string, string, *time.Time, *time.Time) int64); ok {
r0 = rf(_a0, _a1, _a2, _a3, _a4, _a5)
} else {
r0 = ret.Get(0).(int64)
}

var r1 error
if rf, ok := ret.Get(1).(func(context.Context, string, string, *time.Time, *time.Time) error); ok {
r1 = rf(_a0, _a1, _a2, _a3, _a4)
if rf, ok := ret.Get(1).(func(context.Context, string, string, string, *time.Time, *time.Time) error); ok {
r1 = rf(_a0, _a1, _a2, _a3, _a4, _a5)
} else {
r1 = ret.Error(1)
}
Expand Down
10 changes: 7 additions & 3 deletions mnemosyned/postgres_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ func (ps *postgresStorage) SetValue(ctx context.Context, accessToken string, key
}

// Delete implements storage interface.
func (ps *postgresStorage) Delete(ctx context.Context, accessToken, refreshToken string, expiredAtFrom, expiredAtTo *time.Time) (int64, error) {
where, args := ps.where(accessToken, refreshToken, expiredAtFrom, expiredAtTo)
func (ps *postgresStorage) Delete(ctx context.Context, subjectID, accessToken, refreshToken string, expiredAtFrom, expiredAtTo *time.Time) (int64, error) {
where, args := ps.where(subjectID, accessToken, refreshToken, expiredAtFrom, expiredAtTo)
if where.Len() == 0 {
return 0, fmt.Errorf("session cannot be deleted, no where parameter provided: %s", where.String())
}
Expand Down Expand Up @@ -346,12 +346,16 @@ func (ps *postgresStorage) incError(field prometheus.Labels) {
}
}

func (ps *postgresStorage) where(accessToken, refreshToken string, expiredAtFrom, expiredAtTo *time.Time) (*bytes.Buffer, []interface{}) {
func (ps *postgresStorage) where(subjectID, accessToken, refreshToken string, expiredAtFrom, expiredAtTo *time.Time) (*bytes.Buffer, []interface{}) {
var count int
buf := bytes.NewBuffer(nil)
args := make([]interface{}, 0, 4)

switch {
case subjectID != "":
count++
fmt.Fprintf(buf, " subject_id = $%d", count)
args = append(args, subjectID)
case accessToken != "":
count++
fmt.Fprintf(buf, " access_token = $%d", count)
Expand Down
2 changes: 1 addition & 1 deletion mnemosyned/session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ InfLoop:
case <-time.After(sm.ttc):
t := time.Now()
sklog.Debug(logger, "session cleanup start", "start_at", t.Format(time.RFC3339))
affected, err := sm.storage.Delete(context.Background(), "", "", nil, &t)
affected, err := sm.storage.Delete(context.Background(), "", "", "", nil, &t)
if err != nil {
if sm.monitor.enabled {
sm.monitor.cleanup.errors.Inc()
Expand Down
2 changes: 1 addition & 1 deletion mnemosyned/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ type storage interface {
Get(context.Context, string) (*mnemosynerpc.Session, error)
List(context.Context, int64, int64, *time.Time, *time.Time) ([]*mnemosynerpc.Session, error)
Exists(context.Context, string) (bool, error)
Delete(context.Context, string, string, *time.Time, *time.Time) (int64, error)
Delete(context.Context, string, string, string, *time.Time, *time.Time) (int64, error)
SetValue(context.Context, string, string, string) (map[string]string, error)
}
24 changes: 18 additions & 6 deletions mnemosyned/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,21 @@ func testStorage_Delete(t *testing.T, s storage) {

expiredAtTo := time.Now().Add(35 * time.Minute)

affected, err := s.Delete(context.Background(), "", "", nil, &expiredAtTo)
affected, err := s.Delete(context.Background(), "", "", "", nil, &expiredAtTo)
if assert.NoError(t, err) {
assert.Equal(t, nb, affected)
}

data := []struct {
subjectID bool
accessToken bool
refreshToken bool
expiredAtFrom bool
expiredAtTo bool
}{
{
subjectID: true,
},
{
accessToken: true,
},
Expand All @@ -273,6 +277,11 @@ func testStorage_Delete(t *testing.T, s storage) {
accessToken: true,
expiredAtFrom: true,
},
{
subjectID: true,
accessToken: true,
expiredAtFrom: true,
},
{
accessToken: true,
refreshToken: true,
Expand Down Expand Up @@ -319,11 +328,14 @@ DataLoop:
}

var (
accessToken, refreshToken string
expiredAtTo *time.Time
expiredAtFrom *time.Time
subjectID, accessToken, refreshToken string
expiredAtTo *time.Time
expiredAtFrom *time.Time
)

if args.subjectID {
subjectID = ses.SubjectId
}
if args.accessToken {
accessToken = ses.AccessToken
}
Expand All @@ -347,14 +359,14 @@ DataLoop:
expiredAtTo = &eat
}

affected, err = s.Delete(context.Background(), accessToken, refreshToken, expiredAtFrom, expiredAtTo)
affected, err = s.Delete(context.Background(), subjectID, accessToken, refreshToken, expiredAtFrom, expiredAtTo)
if assert.NoError(t, err) {
if assert.Equal(t, int64(1), affected, "one session should be removed for accessToken: %-5t, refreshToken: %-5t, ,expiredAtFrom: %-5t, expiredAtTo: %-5t", args.accessToken, args.refreshToken, args.expiredAtFrom, args.expiredAtTo) {
t.Logf("as expected session can be deleted with arguments accessToken: %-5t, refreshToken: %-5t, expiredAtFrom: %-5t, expiredAtTo: %-5t", args.accessToken, args.refreshToken, args.expiredAtFrom, args.expiredAtTo)
}
}

affected, err = s.Delete(context.Background(), accessToken, refreshToken, expiredAtFrom, expiredAtTo)
affected, err = s.Delete(context.Background(), subjectID, accessToken, refreshToken, expiredAtFrom, expiredAtTo)
if assert.NoError(t, err) {
assert.Equal(t, int64(0), affected)
}
Expand Down
93 changes: 47 additions & 46 deletions mnemosynerpc/session.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions mnemosynerpc/session.proto
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ message DeleteRequest {
google.protobuf.Timestamp expire_at_from = 2;
google.protobuf.Timestamp expire_at_to = 3;
string refresh_token = 4;
string subject_id = 5;
}
message DeleteResponse {
int64 count = 1;
Expand Down

0 comments on commit 883966e

Please sign in to comment.