Fix quota odd behaviour

This commit is contained in:
Adphi 2019-01-16 15:31:22 +01:00
parent 4f50c4817a
commit dd062fe989
6 changed files with 39 additions and 11 deletions

View File

@ -166,7 +166,7 @@ var (
// username := fmt.Sprintf("%s-2", config.NotExistingUser)
// err := c.Users().Create(username, password, nil)
// assert.NoError(t, err)
// user := &types.Users{
// user := &types.UserDetails{
// ID: username,
// Displayname: strings.ToUpper(username),
// Email: "some@address.com",
@ -174,6 +174,10 @@ var (
// Twitter: "@me",
// Phone: "42 42 242 424",
// Website: "my.site.com",
// Quota: types.Quota{
// // Unlimited
// Quota: -3,
// },
// }
// err = c.Users().Update(user)
// assert.NoError(t, err)
@ -314,7 +318,7 @@ var (
{
"TestUserUpdateQuota",
func(t *testing.T) {
quota := 1024 * 1024 * 1024
quota := int64(1024 * 1024 * 1024)
err := c.Users().UpdateQuota(config.NotExistingUser, quota)
assert.NoError(t, err)
// TODO : Find better verification : A never connected Users does not have quota available

View File

@ -113,7 +113,7 @@ type Users interface {
UpdateWebSite(name string, website string) error
UpdateTwitter(name string, twitter string) error
UpdatePassword(name string, password string) error
UpdateQuota(name string, quota int) error
UpdateQuota(name string, quota int64) error
GroupList(name string) ([]string, error)
GroupAdd(name string, group string) error
GroupRemove(name string, group string) error

View File

@ -394,11 +394,11 @@ func (_m *Users) UpdatePhone(name string, phone string) error {
}
// UpdateQuota provides a mock function with given fields: name, quota
func (_m *Users) UpdateQuota(name string, quota int) error {
func (_m *Users) UpdateQuota(name string, quota int64) error {
ret := _m.Called(name, quota)
var r0 error
if rf, ok := ret.Get(0).(func(string, int) error); ok {
if rf, ok := ret.Get(0).(func(string, int64) error); ok {
r0 = rf(name, quota)
} else {
r0 = ret.Error(0)

View File

@ -1,5 +1,7 @@
package types
import "strconv"
//User encapsulate the data needed to create a new Nextcloud's User
type User struct {
Username string
@ -38,3 +40,10 @@ type Quota struct {
Relative float64 `json:"relative"`
Quota int64 `json:"quota"`
}
func (q *Quota) String() string {
if q.Quota < 0 {
return "none"
}
return strconv.FormatInt(q.Quota, 10)
}

View File

@ -10,7 +10,6 @@ import (
"net/http"
"net/url"
"path"
"strconv"
"strings"
"sync"
)
@ -195,8 +194,21 @@ func (u *Users) Update(user *types.UserDetails) error {
errs := make(chan types.UpdateError)
var wg sync.WaitGroup
for k := range m {
if !ignoredUserField(k) && m[k].(string) != "" {
var value string
// Quota is a special case
if k == "Quota" {
// If empty
if user.Quota == (types.Quota{}) {
value = "default"
} else {
value = user.Quota.String()
}
} else {
value = m[k].(string)
}
if !ignoredUserField(k) && value != "" {
wg.Add(1)
// All other non ignored values are strings
go func(key string, value string) {
defer wg.Done()
if err := u.updateAttribute(user.ID, strings.ToLower(key), value); err != nil {
@ -205,7 +217,7 @@ func (u *Users) Update(user *types.UserDetails) error {
Error: err,
}
}
}(k, m[k].(string))
}(k, value)
}
}
go func() {
@ -250,9 +262,10 @@ func (u *Users) UpdatePassword(name string, password string) error {
return u.updateAttribute(name, "password", password)
}
//UpdateQuota update the user's quota (bytes)
func (u *Users) UpdateQuota(name string, quota int) error {
return u.updateAttribute(name, "quota", strconv.Itoa(quota))
//UpdateQuota update the user's quota (bytes). Set negative quota for unlimited
func (u *Users) UpdateQuota(name string, quota int64) error {
q := types.Quota{Quota: quota}
return u.updateAttribute(name, "quota", q.String())
}
//GroupList lists the user's groups

View File

@ -55,5 +55,7 @@ func reformatJSON(json string) string {
json = strings.Replace(json, "\"false\"", "false", -1)
// Nextcloud encode quota as an empty array for never connected users
json = strings.Replace(json, "\"quota\":[],", "", -1)
// Nextcloud send admin unlimited quota as -3, others as "none" : replace with negative value
json = strings.Replace(json, "\"quota\":\"none\"", "\"quota\":-3", -1)
return json
}