/
auth_quota_suite.go
78 lines (68 loc) · 2.43 KB
/
auth_quota_suite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2018 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package storagetest
import (
"github.com/tsuru/tsuru/auth"
"github.com/tsuru/tsuru/types/quota"
check "gopkg.in/check.v1"
)
type userStorage interface {
Create(*auth.User) error
Remove(*auth.User) error
}
type UserQuotaSuite struct {
SuiteHooks
UserStorage userStorage
UserQuotaStorage quota.QuotaStorage
UserQuotaService quota.QuotaService
}
func (s *UserQuotaSuite) TestGet(c *check.C) {
user := &auth.User{Email: "example@example.com", Quota: quota.UnlimitedQuota}
s.UserStorage.Create(user)
quota, err := s.UserQuotaStorage.Get("example@example.com")
c.Assert(err, check.IsNil)
c.Assert(quota.InUse, check.Equals, 0)
c.Assert(quota.Limit, check.Equals, -1)
}
func (s *UserQuotaSuite) TestGetNotFound(c *check.C) {
_, err := s.UserQuotaStorage.Get("myapp")
c.Assert(err, check.NotNil)
c.Assert(err, check.Equals, quota.ErrQuotaNotFound)
}
func (s *UserQuotaSuite) TestInc(c *check.C) {
user := &auth.User{Email: "example@example.com", Quota: quota.Quota{Limit: 5}}
s.UserStorage.Create(user)
err := s.UserQuotaStorage.Inc("example@example.com", 1)
c.Assert(err, check.IsNil)
quota, err := s.UserQuotaStorage.Get("example@example.com")
c.Assert(err, check.IsNil)
c.Assert(quota.InUse, check.Equals, 1)
c.Assert(quota.Limit, check.Equals, 5)
err = s.UserQuotaStorage.Inc("example@example.com", 2)
c.Assert(err, check.IsNil)
quota, err = s.UserQuotaStorage.Get("example@example.com")
c.Assert(err, check.IsNil)
c.Assert(quota.InUse, check.Equals, 3)
c.Assert(quota.Limit, check.Equals, 5)
}
func (s *UserQuotaSuite) TestIncNotFound(c *check.C) {
err := s.UserQuotaStorage.Inc("myapp", 1)
c.Assert(err, check.NotNil)
c.Assert(err, check.Equals, quota.ErrQuotaNotFound)
}
func (s *UserQuotaSuite) TestSetLimit(c *check.C) {
user := &auth.User{Email: "example@example.com", Quota: quota.Quota{Limit: 5}}
s.UserStorage.Create(user)
err := s.UserQuotaStorage.SetLimit("example@example.com", 1)
c.Assert(err, check.IsNil)
quota, err := s.UserQuotaStorage.Get("example@example.com")
c.Assert(err, check.IsNil)
c.Assert(quota.InUse, check.Equals, 0)
c.Assert(quota.Limit, check.Equals, 1)
}
func (s *UserQuotaSuite) TestSetLimitNotFound(c *check.C) {
err := s.UserQuotaStorage.SetLimit("myapp", 1)
c.Assert(err, check.NotNil)
c.Assert(err, check.Equals, quota.ErrQuotaNotFound)
}