Tests restructuration and AppsConfig implementation

This commit is contained in:
Adphi 2018-10-16 14:38:32 +02:00
parent f40f6afa5c
commit 1cc321d657
6 changed files with 267 additions and 76 deletions

106
appsconfig.go Normal file
View File

@ -0,0 +1,106 @@
package gonextcloud
import (
req "github.com/levigross/grequests"
"gitlab.adphi.fr/partitio/Nextcloud-Partitio/gonextcloud/types"
"net/http"
"sync"
)
//AppsConfigList lists all the available apps
func (c *Client) AppsConfigList() (apps []string, err error) {
res, err := c.baseRequest(http.MethodGet, routes.appsConfig, nil)
if err != nil {
return nil, err
}
var r types.AppConfigResponse
res.JSON(&r)
return r.Ocs.Data.Data, nil
}
//AppsConfigKeys returns the app's config keys
func (c *Client) AppsConfigKeys(id string) (keys []string, err error) {
res, err := c.baseRequest(http.MethodGet, routes.appsConfig, nil, id)
if err != nil {
return nil, err
}
var r types.AppConfigResponse
res.JSON(&r)
return r.Ocs.Data.Data, nil
}
//AppsConfigValue get the config value for the given app's key
func (c *Client) AppsConfigValue(id, key string) (string, error) {
res, err := c.baseRequest(http.MethodGet, routes.appsConfig, nil, id, key)
if err != nil {
return "", err
}
var r types.AppcConfigValueResponse
res.JSON(&r)
return r.Ocs.Data.Data, nil
}
//AppsConfigSetValue set the config value for the given app's key
func (c *Client) AppsConfigSetValue(id, key, value string) error {
ro := &req.RequestOptions{
Data: map[string]string{
"value": value,
},
}
_, err := c.baseRequest(http.MethodPost, routes.appsConfig, ro, id, key)
return err
}
//AppsConfigDeleteValue delete the config value and (!! be careful !!) the key
func (c *Client) AppsConfigDeleteValue(id, key, value string) error {
_, err := c.baseRequest(http.MethodDelete, routes.appsConfig, nil, id, key)
return err
}
//AppsConfig returns all apps AppConfigDetails
func (c *Client) AppsConfig() (map[string]map[string]string, error) {
config := make(map[string]map[string]string)
var err error
appsIDs, err := c.AppsConfigList()
if err != nil {
return nil, err
}
var wg sync.WaitGroup
wg.Add(len(appsIDs))
for i := range appsIDs {
go func(id string) {
defer wg.Done()
d, err := c.AppsConfigDetails(id)
if err == nil {
config[id] = d
}
}(appsIDs[i])
}
wg.Wait()
return config, err
}
//AppsConfigDetails returns all the config's key, values pair of the app
func (c *Client) AppsConfigDetails(appID string) (map[string]string, error) {
config := make(map[string]string)
var err error
var ks []string
ks, err = c.AppsConfigKeys(appID)
if err != nil {
return config, err
}
var wg sync.WaitGroup
wg.Add(len(ks))
for i := range ks {
go func(key string) {
defer wg.Done()
var v string
v, err = c.AppsConfigValue(appID, key)
if err == nil {
config[key] = v
}
}(ks[i])
}
wg.Wait()
return config, err
}

56
appsconfig_test.go Normal file
View File

@ -0,0 +1,56 @@
package gonextcloud
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestAppsConfig(t *testing.T) {
c = nil
if err := initClient(); err != nil {
t.Fatal(err)
}
ac, err := c.AppsConfig()
assert.NoError(t, err)
assert.NotEmpty(t, ac)
}
func TestAppsConfigList(t *testing.T) {
c = nil
if err := initClient(); err != nil {
t.Fatal(err)
}
a, err := c.AppsConfigList()
assert.NoError(t, err)
assert.Contains(t, a, "files")
}
func TestAppsConfigKeys(t *testing.T) {
c = nil
if err := initClient(); err != nil {
t.Fatal(err)
}
ks, err := c.AppsConfigKeys("activity")
assert.NoError(t, err)
assert.Contains(t, ks, "enabled")
}
func TestAppsConfigValue(t *testing.T) {
c = nil
if err := initClient(); err != nil {
t.Fatal(err)
}
k, err := c.AppsConfigValue("files", "enabled")
assert.NoError(t, err)
assert.Equal(t, "yes", k)
}
func TestAppConfigDetails(t *testing.T) {
c = nil
if err := initClient(); err != nil {
t.Fatal(err)
}
d, err := c.AppsConfigDetails("activity")
assert.NoError(t, err)
assert.NotEmpty(t, d)
}

View File

@ -463,72 +463,6 @@ var (
},
},
}
groupFoldersTests = []struct {
string
test
}{
{
"TestGroupFoldersCreate",
func(t *testing.T) {
// Recreate client
var err error
groupID, err = c.GroupFoldersCreate("API")
assert.NoError(t, err)
},
},
{
"TestGroupFoldersList",
func(t *testing.T) {
gfs, err := c.GroupFoldersList()
assert.NoError(t, err)
assert.NotNil(t, gfs[groupID])
},
},
{
"TestGroupFolders",
func(t *testing.T) {
gf, err := c.GroupFolders(groupID)
assert.NoError(t, err)
assert.NotNil(t, gf)
},
},
{
"TestGroupFolderRename",
func(t *testing.T) {
err := c.GroupFoldersRename(groupID, "API_Renamed")
assert.NoError(t, err)
},
},
{
"TestGroupFoldersAddGroup",
func(t *testing.T) {
err := c.GroupFoldersAddGroup(groupID, "admin")
assert.NoError(t, err)
},
},
{
"TestGroupFoldersSetGroupPermissions",
func(t *testing.T) {
err := c.GroupFoldersSetGroupPermissions(groupID, "admin", types.ReadPermission)
assert.NoError(t, err)
},
},
{
"TestGroupFoldersSetQuota",
func(t *testing.T) {
err := c.GroupFoldersSetQuota(groupID, 100)
assert.NoError(t, err)
},
},
{
"TestGroupFolderRemoveGroup",
func(t *testing.T) {
err := c.GroupFoldersRemoveGroup(groupID, "admin")
assert.NoError(t, err)
},
},
}
)
func TestClient(t *testing.T) {
@ -540,16 +474,6 @@ func TestClient(t *testing.T) {
}
}
func TestGroupFolders(t *testing.T) {
c = nil
if err := initClient(); err != nil {
t.Fatal(err)
}
for _, tt := range groupFoldersTests {
t.Run(tt.string, tt.test)
}
}
func TestUserCreateWithoutPassword(t *testing.T) {
c = nil
if err := initClient(); err != nil {

85
groupfolders_test.go Normal file
View File

@ -0,0 +1,85 @@
package gonextcloud
import (
"github.com/stretchr/testify/assert"
"gitlab.adphi.fr/partitio/Nextcloud-Partitio/gonextcloud/types"
"testing"
)
var (
groupFoldersTests = []struct {
string
test
}{
{
"TestGroupFoldersCreate",
func(t *testing.T) {
// Recreate client
var err error
groupID, err = c.GroupFoldersCreate("API")
assert.NoError(t, err)
},
},
{
"TestGroupFoldersList",
func(t *testing.T) {
gfs, err := c.GroupFoldersList()
assert.NoError(t, err)
assert.NotNil(t, gfs[groupID])
},
},
{
"TestGroupFolders",
func(t *testing.T) {
gf, err := c.GroupFolders(groupID)
assert.NoError(t, err)
assert.NotNil(t, gf)
},
},
{
"TestGroupFolderRename",
func(t *testing.T) {
err := c.GroupFoldersRename(groupID, "API_Renamed")
assert.NoError(t, err)
},
},
{
"TestGroupFoldersAddGroup",
func(t *testing.T) {
err := c.GroupFoldersAddGroup(groupID, "admin")
assert.NoError(t, err)
},
},
{
"TestGroupFoldersSetGroupPermissions",
func(t *testing.T) {
err := c.GroupFoldersSetGroupPermissions(groupID, "admin", types.ReadPermission)
assert.NoError(t, err)
},
},
{
"TestGroupFoldersSetQuota",
func(t *testing.T) {
err := c.GroupFoldersSetQuota(groupID, 100)
assert.NoError(t, err)
},
},
{
"TestGroupFolderRemoveGroup",
func(t *testing.T) {
err := c.GroupFoldersRemoveGroup(groupID, "admin")
assert.NoError(t, err)
},
},
}
)
func TestGroupFolders(t *testing.T) {
c = nil
if err := initClient(); err != nil {
t.Fatal(err)
}
for _, tt := range groupFoldersTests {
t.Run(tt.string, tt.test)
}
}

View File

@ -11,6 +11,7 @@ type Routes struct {
monitor *url.URL
shares *url.URL
groupfolders *url.URL
appsConfig *url.URL
}
const badRequest = 998
@ -25,5 +26,6 @@ var (
monitor: &url.URL{Path: apiPath.Path + "/apps/serverinfo/api/v1/info"},
shares: &url.URL{Path: apiPath.Path + "/apps/files_sharing/api/v1/shares"},
groupfolders: &url.URL{Path: "apps/groupfolders/folders"},
appsConfig: &url.URL{Path: apiPath.Path + "/apps/provisioning_api/api/v1/config/apps"},
}
)

View File

@ -90,6 +90,24 @@ type AppResponse struct {
} `json:"ocs"`
}
type AppConfigResponse struct {
Ocs struct {
Meta Meta `json:"meta"`
Data struct {
Data []string `json:"data"`
} `json:"data"`
} `json:"ocs"`
}
type AppcConfigValueResponse struct {
Ocs struct {
Meta Meta `json:"meta"`
Data struct {
Data string `json:"data"`
} `json:"data"`
} `json:"ocs"`
}
//CapabilitiesResponse
type CapabilitiesResponse struct {
Ocs struct {