Added User related tests

This commit is contained in:
Philippe-Adrien Nousse 2018-07-24 20:26:12 +02:00
parent d338b0c67a
commit 40718cb98c
12 changed files with 478 additions and 82 deletions

View File

@ -1 +1,35 @@
package client
import (
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/client/types"
)
func (c *Client) AppList() ([]string, error) {
if !c.loggedIn() {
return nil, unauthorized
}
u := c.baseURL.ResolveReference(routes.apps)
res, err := c.session.Get(u.String(), nil)
if err != nil {
return nil, err
}
var r types.AppListResponse
res.JSON(&r)
return r.Ocs.Data.Apps, nil
return nil, nil
}
func (c *Client) appsBaseRequest(name string, route string, ro *req.RequestOptions, method string) error {
res, err := c.baseRequest(routes.apps, name, route, ro, method)
if err != nil {
return err
}
var r types.UserResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return &e
}
return nil
}

View File

@ -18,16 +18,22 @@ func (c *Client) Login(username string, password string) error {
c.session = req.NewSession(&options)
// TODO What to do with capabilities ? (other thant connection validation)
u := c.baseURL.ResolveReference(routes.capabilities)
r, err := c.session.Get(u.String(), nil)
res, err := c.session.Get(u.String(), nil)
if err != nil {
return err
}
var cs types.CapabilitiesResponse
r.JSON(&cs)
if cs.Ocs.Meta.Statuscode != 100 {
return fmt.Errorf("%d : %s", cs.Ocs.Meta.Statuscode, cs.Ocs.Meta.Message)
var r types.CapabilitiesResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return &e
}
c.capabilities = &r.Ocs.Data.Capabilities
// Check if authentication failed
if !c.loggedIn() {
e := types.APIError{Message: "authentication failed"}
return &e
}
c.capabilities = &cs.Ocs.Data.Capabilities
return nil
}
@ -37,5 +43,6 @@ func (c *Client) Logout() error {
}
func (c *Client) loggedIn() bool {
return c.capabilities != nil
// When authentication failed, capabilities doesn't contains core information
return c.capabilities.Core.WebdavRoot != ""
}

View File

@ -1,7 +1,6 @@
package client
import (
"fmt"
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/client/types"
"net/http"
@ -12,12 +11,13 @@ func (c *Client) GroupList() ([]string, error) {
if err != nil {
return nil, err
}
var gr types.GroupListResponse
res.JSON(&gr)
if gr.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf("%d : %s", gr.Ocs.Meta.Statuscode, gr.Ocs.Meta.Message)
var r types.GroupListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return gr.Ocs.Data.Groups, nil
return r.Ocs.Data.Groups, nil
}
func (c *Client) GroupUsers(name string) ([]string, error) {
@ -28,7 +28,8 @@ func (c *Client) GroupUsers(name string) ([]string, error) {
var r types.UserListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf("%d : %s", r.Ocs.Meta.Statuscode, r.Ocs.Meta.Message)
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data.Users, nil
}
@ -44,7 +45,8 @@ func (c *Client) GroupSearch(search string) ([]string, error) {
var r types.GroupListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf("%d : %s", r.Ocs.Meta.Statuscode, r.Ocs.Meta.Message)
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data.Groups, nil
}
@ -76,7 +78,8 @@ func (c *Client) GroupSubAdminList(name string) ([]string, error) {
var r types.UserListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf("%d : %s", r.Ocs.Meta.Statuscode, r.Ocs.Meta.Message)
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data.Users, nil
}
@ -86,10 +89,11 @@ func (c *Client) groupBaseRequest(name string, route string, ro *req.RequestOpti
if err != nil {
return err
}
var ur types.GroupListResponse
res.JSON(&ur)
if ur.Ocs.Meta.Statuscode != 100 {
return fmt.Errorf("%d : %s", ur.Ocs.Meta.Statuscode, ur.Ocs.Meta.Message)
var r types.GroupListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return &e
}
return nil
}

View File

@ -38,4 +38,9 @@ type BaseClient interface {
GroupCreate(name string) error
GroupDelete(name string) error
GroupSubAdminList(name string) ([]string, error)
AppList() ([]string, error)
App(name string) (types.App, error)
AppEnable(name string) error
AppDisable(name string) error
}

View File

@ -6,14 +6,17 @@ type Routes struct {
capabilities *url.URL
users *url.URL
groups *url.URL
apps *url.URL
}
const badRequest = 998
var (
apiPath = &url.URL{Path: "/ocs/v1.php/cloud"}
routes = Routes{
capabilities: &url.URL{Path: apiPath.Path + "/capabilities"},
users: &url.URL{Path: apiPath.Path + "/users"},
groups: &url.URL{Path: apiPath.Path + "/groups"},
apps: &url.URL{Path: apiPath.Path + "/apps"},
}
badRequest = 998
)

67
client/types/app.go Normal file
View File

@ -0,0 +1,67 @@
package types
type App struct {
ID string `json:"id"`
Ocsid string `json:"ocsid"`
Name string `json:"name"`
Summary string `json:"summary"`
Description string `json:"description"`
Licence string `json:"licence"`
Author string `json:"author"`
Version string `json:"version"`
Namespace string `json:"namespace"`
Types []string `json:"types"`
Documentation struct {
Admin string `json:"admin"`
Developer string `json:"developer"`
User string `json:"user"`
} `json:"documentation"`
Category []string `json:"category"`
Website string `json:"website"`
Bugs string `json:"bugs"`
Repository struct {
Attributes struct {
Type string `json:"type"`
} `json:"@attributes"`
Value string `json:"@value"`
} `json:"repository"`
Screenshot []interface{} `json:"screenshot"`
Dependencies struct {
Owncloud struct {
Attributes struct {
MinVersion string `json:"min-version"`
MaxVersion string `json:"max-version"`
} `json:"@attributes"`
} `json:"owncloud"`
Nextcloud struct {
Attributes struct {
MinVersion string `json:"min-version"`
MaxVersion string `json:"max-version"`
} `json:"@attributes"`
} `json:"nextcloud"`
} `json:"dependencies"`
Settings struct {
Admin []string `json:"admin"`
AdminSection []string `json:"admin-section"`
Personal []interface{} `json:"personal"`
PersonalSection []interface{} `json:"personal-section"`
} `json:"settings"`
Info []interface{} `json:"info"`
Remote []interface{} `json:"remote"`
Public []interface{} `json:"public"`
RepairSteps struct {
Install []interface{} `json:"install"`
PreMigration []interface{} `json:"pre-migration"`
PostMigration []interface{} `json:"post-migration"`
LiveMigration []interface{} `json:"live-migration"`
Uninstall []interface{} `json:"uninstall"`
} `json:"repair-steps"`
BackgroundJobs []interface{} `json:"background-jobs"`
TwoFactorProviders []interface{} `json:"two-factor-providers"`
Commands []interface{} `json:"commands"`
Activity struct {
Filters []interface{} `json:"filters"`
Settings []interface{} `json:"settings"`
Providers []interface{} `json:"providers"`
} `json:"activity"`
}

19
client/types/errors.go Normal file
View File

@ -0,0 +1,19 @@
package types
import "fmt"
type APIError struct {
Code int
Message string
}
func ErrorFromMeta(meta Meta) APIError {
return APIError{
meta.Statuscode,
meta.Message,
}
}
func (e *APIError) Error() string {
return fmt.Sprintf("%d : %s", e.Code, e.Message)
}

View File

@ -1,27 +1,23 @@
package types
type Meta struct {
Status string `json:"status"`
Statuscode int `json:"statuscode"`
Message string `json:"message"`
Totalitems string `json:"totalitems"`
Itemsperpage string `json:"itemsperpage"`
}
type ErrorResponse struct {
Ocs struct {
Meta struct {
Status string `json:"status"`
Statuscode int `json:"statuscode"`
Message string `json:"message"`
Totalitems string `json:"totalitems"`
Itemsperpage string `json:"itemsperpage"`
} `json:"meta"`
Meta Meta `json:"meta"`
Data []interface{} `json:"data"`
} `json:"ocs"`
}
type UserListResponse struct {
Ocs struct {
Meta struct {
Status string `json:"status"`
Statuscode int `json:"statuscode"`
Message string `json:"message"`
Totalitems string `json:"totalitems"`
Itemsperpage string `json:"itemsperpage"`
} `json:"meta"`
Meta Meta `json:"meta"`
Data struct {
Users []string `json:"users"`
} `json:"data"`
@ -30,54 +26,46 @@ type UserListResponse struct {
type UserResponse struct {
Ocs struct {
Meta struct {
Status string `json:"status"`
Statuscode int `json:"statuscode"`
Message string `json:"message"`
Totalitems string `json:"totalitems"`
Itemsperpage string `json:"itemsperpage"`
} `json:"meta"`
Meta Meta `json:"meta"`
Data User `json:"data"`
} `json:"ocs"`
}
type BaseResponse struct {
Ocs struct {
Meta struct {
Status string `json:"status"`
Statuscode int `json:"statuscode"`
Message string `json:"message"`
Totalitems string `json:"totalitems"`
Itemsperpage string `json:"itemsperpage"`
} `json:"meta"`
Meta Meta `json:"meta"`
Data []string `json:"data"`
} `json:"ocs"`
}
type GroupListResponse struct {
Ocs struct {
Meta struct {
Status string `json:"status"`
Statuscode int `json:"statuscode"`
Message string `json:"message"`
Totalitems string `json:"totalitems"`
Itemsperpage string `json:"itemsperpage"`
} `json:"meta"`
Meta Meta `json:"meta"`
Data struct {
Groups []string `json:"groups"`
} `json:"data"`
} `json:"ocs"`
}
type AppListResponse struct {
Ocs struct {
Meta Meta `json:"meta"`
Data struct {
Apps []string `json:"apps"`
} `json:"data"`
} `json:"ocs"`
}
type AppResponse struct {
Ocs struct {
Meta Meta `json:"meta"`
Data App `json:"data"`
} `json:"ocs"`
}
type CapabilitiesResponse struct {
Ocs struct {
Meta struct {
Status string `json:"status"`
Statuscode int `json:"statuscode"`
Message string `json:"message"`
Totalitems string `json:"totalitems"`
Itemsperpage string `json:"itemsperpage"`
} `json:"meta"`
Meta Meta `json:"meta"`
Data struct {
Version struct {
Major int `json:"major"`

View File

@ -1,7 +1,7 @@
package types
type User struct {
Enabled string `json:"enabled"`
Enabled bool `json:"enabled"`
ID string `json:"id"`
Quota struct {
Free int64 `json:"free"`

View File

@ -1,11 +1,12 @@
package client
import (
"fmt"
"encoding/json"
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/client/types"
"net/http"
"path"
"strconv"
)
func (c *Client) UserList() ([]string, error) {
@ -17,12 +18,15 @@ func (c *Client) UserList() ([]string, error) {
if err != nil {
return nil, err
}
var ul types.UserListResponse
res.JSON(&ul)
return ul.Ocs.Data.Users, nil
var r types.UserListResponse
res.JSON(&r)
return r.Ocs.Data.Users, nil
}
func (c *Client) User(name string) (*types.User, error) {
if name == "" {
return nil, &types.APIError{Message: "name cannot be empty"}
}
if !c.loggedIn() {
return nil, unauthorized
}
@ -32,12 +36,18 @@ func (c *Client) User(name string) (*types.User, error) {
if err != nil {
return nil, err
}
var ur types.UserResponse
res.JSON(&ur)
if ur.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf("%d : %s", ur.Ocs.Meta.Statuscode, ur.Ocs.Meta.Message)
var r types.UserResponse
js := res.String()
// Nextcloud does not encode JSON properly
js = reformatJSON(js)
if err := json.Unmarshal([]byte(js), &r); err != nil {
return nil, err
}
return &ur.Ocs.Data, nil
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return &r.Ocs.Data, nil
}
func (c *Client) UserSearch(search string) ([]string, error) {
@ -55,7 +65,8 @@ func (c *Client) UserSearch(search string) ([]string, error) {
var r types.UserListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf("%d : %s", r.Ocs.Meta.Statuscode, r.Ocs.Meta.Message)
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data.Users, nil
}
@ -120,8 +131,8 @@ func (c *Client) UserUpdatePassword(name string, password string) error {
return c.userUpdateAttribute(name, "password", password)
}
func (c *Client) UserUpdateQuota(name string, quota string) error {
return c.userUpdateAttribute(name, "quota", quota)
func (c *Client) UserUpdateQuota(name string, quota int) error {
return c.userUpdateAttribute(name, "quota", strconv.Itoa(quota))
}
func (c *Client) UserGroupList(name string) ([]string, error) {
@ -137,7 +148,8 @@ func (c *Client) UserGroupList(name string) ([]string, error) {
var r types.GroupListResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf("%d : %s", r.Ocs.Meta.Statuscode, r.Ocs.Meta.Message)
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data.Groups, nil
}
@ -191,7 +203,8 @@ func (c *Client) UserGroupSubAdminList(name string) ([]string, error) {
var r types.BaseResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
return nil, fmt.Errorf("%d : %s", r.Ocs.Meta.Statuscode, r.Ocs.Meta.Message)
e := types.ErrorFromMeta(r.Ocs.Meta)
return nil, &e
}
return r.Ocs.Data, nil
}
@ -211,10 +224,11 @@ func (c *Client) userBaseRequest(name string, route string, ro *req.RequestOptio
if err != nil {
return err
}
var ur types.UserResponse
res.JSON(&ur)
if ur.Ocs.Meta.Statuscode != 100 {
return fmt.Errorf("%d : %s", ur.Ocs.Meta.Statuscode, ur.Ocs.Meta.Message)
var r types.UserResponse
res.JSON(&r)
if r.Ocs.Meta.Statuscode != 100 {
e := types.ErrorFromMeta(r.Ocs.Meta)
return &e
}
return nil
}

View File

@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"path"
"strings"
)
func (c *Client) baseRequest(route *url.URL, name string, subroute string, ro *req.RequestOptions, method string) (*req.Response, error) {
@ -36,3 +37,12 @@ func (c *Client) baseRequest(route *url.URL, name string, subroute string, ro *r
}
return res, nil
}
func reformatJSON(json string) string {
// Nextcloud encode boolean as string
json = strings.Replace(json, "\"true\"", "true", -1)
json = strings.Replace(json, "\"false\"", "false", -1)
// Nextcloud encode quota as an empty array for never connected users
json = strings.Replace(json, "\"quota\":[],", "", -1)
return json
}

245
gonextcloud_test.go Normal file
View File

@ -0,0 +1,245 @@
package gonextcloud
import (
"github.com/partitio/gonextcloud/client"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"testing"
)
var config = Config{}
var c *client.Client
type Config struct {
URL string `yaml:"url"`
Login string `yaml:"login"`
Password string `yaml:"password"`
AppName string `yaml:"app-name"`
GroupsToCreate []string `yaml:"groups-to-create"`
NotExistingUser string `yaml:"not-existing-user"`
NotExistingGroup string `yaml:"not-existing-group"`
}
func LoadConfig() error {
f, err := os.Open("./config.yml")
if err != nil {
return err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
if err := yaml.Unmarshal(b, &config); err != nil {
return err
}
return nil
}
func TestTruth(t *testing.T) {
assert.Equal(t, true, true, "seriously ??!")
}
func TestLoadConfig(t *testing.T) {
err := LoadConfig()
assert.Nil(t, err)
}
func TestClient(t *testing.T) {
var err error
c, err = client.NewClient(config.URL)
assert.Nil(t, err, "aie")
}
func TestLoginFail(t *testing.T) {
err := c.Login("", "")
assert.NotNil(t, err)
}
func TestLogin(t *testing.T) {
err := c.Login(config.Login, config.Password)
assert.Nil(t, err)
}
func TestUserList(t *testing.T) {
us, err := c.UserList()
assert.Nil(t, err)
assert.Contains(t, us, config.Login)
}
func TestExistingUser(t *testing.T) {
u, err := c.User(config.Login)
assert.Nil(t, err)
assert.NotNil(t, u)
}
func TestEmptyUser(t *testing.T) {
u, err := c.User("")
assert.NotNil(t, err)
assert.Empty(t, u)
}
func TestNonExistingUser(t *testing.T) {
_, err := c.User(config.NotExistingUser)
assert.NotNil(t, err)
}
func TestUserSearch(t *testing.T) {
us, err := c.UserSearch(config.Login)
assert.Nil(t, err)
assert.Contains(t, us, config.Login)
}
func TestUserCreate(t *testing.T) {
err := c.UserCreate(config.NotExistingUser, "somecomplicatedpassword")
assert.Nil(t, err)
}
func TestUserCreateExisting(t *testing.T) {
err := c.UserCreate(config.NotExistingUser, "somecomplicatedpassword")
assert.NotNil(t, err)
}
func TestGroupList(t *testing.T) {
gs, err := c.GroupList()
assert.Nil(t, err)
assert.Contains(t, gs, "admin")
}
func TestGroupCreate(t *testing.T) {
err := c.GroupCreate(config.NotExistingGroup)
assert.Nil(t, err)
}
func TestUserUpdateEmail(t *testing.T) {
email := "my@mail.com"
err := c.UserUpdateEmail(config.NotExistingUser, email)
assert.Nil(t, err)
u, err := c.User(config.NotExistingUser)
assert.Nil(t, err)
assert.Equal(t, email, u.Email)
}
func TestUserUpdateDisplayName(t *testing.T) {
displayName := "Display Name"
err := c.UserUpdateDisplayName(config.NotExistingUser, displayName)
assert.Nil(t, err)
u, err := c.User(config.NotExistingUser)
assert.Nil(t, err)
assert.Equal(t, displayName, u.Displayname)
}
func TestUserUpdatePhone(t *testing.T) {
phone := "+33 42 42 42 42"
err := c.UserUpdatePhone(config.NotExistingUser, phone)
assert.Nil(t, err)
u, err := c.User(config.NotExistingUser)
assert.Nil(t, err)
assert.Equal(t, phone, u.Phone)
}
func TestUserUpdateAddress(t *testing.T) {
address := "Main Street, Galifrey"
err := c.UserUpdateAddress(config.NotExistingUser, address)
assert.Nil(t, err)
u, err := c.User(config.NotExistingUser)
assert.Nil(t, err)
assert.Equal(t, address, u.Address)
}
func TestUserUpdateWebSite(t *testing.T) {
website := "www.doctor.who"
err := c.UserUpdateWebSite(config.NotExistingUser, website)
assert.Nil(t, err)
u, err := c.User(config.NotExistingUser)
assert.Nil(t, err)
assert.Equal(t, website, u.Website)
}
func TestUserUpdateTwitter(t *testing.T) {
twitter := "@doctorwho"
err := c.UserUpdateTwitter(config.NotExistingUser, twitter)
assert.Nil(t, err)
u, err := c.User(config.NotExistingUser)
assert.Nil(t, err)
assert.Equal(t, twitter, u.Twitter)
}
func TestUserUpdateQuota(t *testing.T) {
quota := 1024 * 1024 * 1024
err := c.UserUpdateQuota(config.NotExistingUser, quota)
assert.Nil(t, err)
// TODO : Find better verification : A never connected User does not have quota available
//u, err := c.User(config.NotExistingUser)
//assert.Nil(t, err)
//assert.Equal(t, quota, u.Quota.Quota)
}
func TestUserUpdatePassword(t *testing.T) {
password := "newcomplexpassword"
err := c.UserUpdatePassword(config.NotExistingUser, password)
assert.Nil(t, err)
}
func TestUserGroupAdd(t *testing.T) {
err := c.UserGroupAdd(config.NotExistingUser, config.NotExistingGroup)
assert.Nil(t, err)
gs, err := c.UserGroupList(config.NotExistingUser)
assert.Nil(t, err)
assert.Contains(t, gs, config.NotExistingGroup)
}
func TestUserGroupSubAdminList(t *testing.T) {
gs, err := c.UserGroupSubAdminList(config.NotExistingUser)
assert.NotNil(t, err)
assert.Empty(t, gs)
}
func TestUserGroupPromote(t *testing.T) {
err := c.UserGroupPromote(config.NotExistingUser, config.NotExistingGroup)
assert.Nil(t, err)
gs, err := c.UserGroupSubAdminList(config.NotExistingUser)
assert.Nil(t, err)
assert.Contains(t, gs, config.NotExistingGroup)
}
func TestUserGroupDemote(t *testing.T) {
err := c.UserGroupDemote(config.NotExistingUser, config.NotExistingGroup)
assert.Nil(t, err)
//gs, err := c.UserGroupSubAdminList(config.NotExistingUser)
//assert.Nil(t, err)
//assert.Empty(t, gs)
}
func TestUserDisable(t *testing.T) {
err := c.UserDisable(config.NotExistingUser)
assert.Nil(t, err)
u, err := c.User(config.NotExistingUser)
assert.Nil(t, err)
assert.False(t, u.Enabled)
}
func TestUserEnable(t *testing.T) {
err := c.UserEnable(config.NotExistingUser)
assert.Nil(t, err)
u, err := c.User(config.NotExistingUser)
assert.Nil(t, err)
assert.True(t, u.Enabled)
}
func TestGroupDelete(t *testing.T) {
err := c.GroupDelete(config.NotExistingGroup)
assert.Nil(t, err)
}
func TestUserDelete(t *testing.T) {
err := c.UserDelete(config.NotExistingUser)
assert.Nil(t, err)
}
func TestLogout(t *testing.T) {
err := c.Logout()
assert.Nil(t, err)
}