#6 Add method creating user only with username and email

This commit is contained in:
Adphi 2018-10-16 11:02:04 +02:00
parent 74ee331f4f
commit 68f06ac164
9 changed files with 130 additions and 9 deletions

View File

@ -27,6 +27,7 @@ func (c *Client) Login(username string, password string) error {
res.JSON(&r)
// No need to check for Ocs.Meta.StatusCode as capabilities are always returned
c.capabilities = &r.Ocs.Data.Capabilities
c.version = &r.Ocs.Data.Version
// Check if authentication failed
if !c.loggedIn() {
e := types.APIError{Message: "authentication failed"}

View File

@ -14,6 +14,7 @@ type Client struct {
session *req.Session
headers map[string]string
capabilities *types.Capabilities
version *types.Version
}
// NewClient create a new Client from the Nextcloud Instance URL

View File

@ -4,4 +4,5 @@ password: mypassword
app-name: testapp
share-folder: /Documents
not-existing-user: this-user-should-not-exist
not-existing-group: this-group-should-not-exist
not-existing-group: this-group-should-not-exist
email: my@mail.com

View File

@ -8,6 +8,7 @@ import (
"net/http"
"net/url"
"os"
"strings"
"testing"
)
@ -19,6 +20,7 @@ type Config struct {
ShareFolder string `yaml:"share-folder"`
NotExistingUser string `yaml:"not-existing-user"`
NotExistingGroup string `yaml:"not-existing-group"`
Email string `yaml:"email"`
}
const password = "somecomplicatedpassword"
@ -544,6 +546,37 @@ func TestGroupFolders(t *testing.T) {
}
}
func TestUserCreateWithoutPassword(t *testing.T) {
c = nil
if err := initClient(); err != nil {
t.Fatal(err)
}
err := c.UserCreateWithoutPassword(config.NotExistingUser, config.Email, strings.Title(config.NotExistingUser))
assert.NoError(t, err)
err = c.UserDelete(config.NotExistingUser)
assert.NoError(t, err)
}
func TestUserListDetails(t *testing.T) {
c = nil
if err := initClient(); err != nil {
t.Fatal(err)
}
us, err := c.UserListDetails()
assert.NoError(t, err)
assert.Contains(t, us, config.Login)
}
func TestGroupListDetails(t *testing.T) {
c = nil
if err := initClient(); err != nil {
t.Fatal(err)
}
gs, err := c.GroupListDetails()
assert.NoError(t, err)
assert.NotEmpty(t, gs)
}
// LoadConfig loads the test configuration
func LoadConfig() error {
f, err := os.Open("./config.yml")

View File

@ -17,6 +17,17 @@ func (c *Client) GroupList() ([]string, error) {
return r.Ocs.Data.Groups, nil
}
//GroupListDetails lists the Nextcloud groups
func (c *Client) GroupListDetails() ([]types.Group, error) {
res, err := c.baseRequest(http.MethodGet, routes.groups, nil, "details")
if err != nil {
return nil, err
}
var r types.GroupListDetailsResponse
res.JSON(&r)
return r.Ocs.Data.Groups, nil
}
//GroupUsers list the group's users
func (c *Client) GroupUsers(name string) ([]string, error) {
res, err := c.baseRequest(http.MethodGet, routes.groups, nil, name)

11
types/group.go Normal file
View File

@ -0,0 +1,11 @@
package types
//Group
type Group struct {
ID string `json:"id"`
Displayname string `json:"displayname"`
UserCount int `json:"usercount"`
Disabled int `json:"disabled"`
CanAdd bool `json:"canAdd"`
CanRemove bool `json:"canRemove"`
}

View File

@ -27,6 +27,15 @@ type UserListResponse struct {
} `json:"ocs"`
}
type UserListDetailsResponse struct {
Ocs struct {
Meta Meta `json:"meta"`
Data struct {
Users map[string]User `json:"users"`
} `json:"data"`
} `json:"ocs"`
}
//UserResponse
type UserResponse struct {
Ocs struct {
@ -53,6 +62,16 @@ type GroupListResponse struct {
} `json:"ocs"`
}
//GroupListDetailsResponse
type GroupListDetailsResponse struct {
Ocs struct {
Meta Meta `json:"meta"`
Data struct {
Groups []Group `json:"groups"`
} `json:"data"`
} `json:"ocs"`
}
//AppListResponse
type AppListResponse struct {
Ocs struct {
@ -76,18 +95,20 @@ type CapabilitiesResponse struct {
Ocs struct {
Meta Meta `json:"meta"`
Data struct {
Version struct {
Major int `json:"major"`
Minor int `json:"minor"`
Micro int `json:"micro"`
String string `json:"string"`
Edition string `json:"edition"`
} `json:"version"`
Version Version `json:"version"`
Capabilities Capabilities `json:"capabilities"`
} `json:"data"`
} `json:"ocs"`
}
type Version struct {
Major int `json:"major"`
Minor int `json:"minor"`
Micro int `json:"micro"`
String string `json:"string"`
Edition string `json:"edition"`
}
type MonitoringResponse struct {
Ocs struct {
Meta Meta `json:"meta"`

View File

@ -18,5 +18,11 @@ type User struct {
Website string `json:"website"`
Twitter string `json:"twitter"`
Groups []string `json:"groups"`
Language string `json:"language"`
Language string `json:"language,omitempty"`
StorageLocation string `json:"storageLocation,omitempty"`
LastLogin int64 `json:"lastLogin,omitempty"`
Backend string `json:"backend,omitempty"`
Subadmin []interface{} `json:"subadmin,omitempty"`
Locale string `json:"locale,omitempty"`
}

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"github.com/fatih/structs"
req "github.com/levigross/grequests"
"github.com/pkg/errors"
"gitlab.adphi.fr/partitio/Nextcloud-Partitio/gonextcloud/types"
"net/http"
"path"
@ -24,6 +25,18 @@ func (c *Client) UserList() ([]string, error) {
return r.Ocs.Data.Users, nil
}
//UserListDetails return a map of user with details
func (c *Client) UserListDetails() (map[string]types.User, error) {
res, err := c.baseRequest(http.MethodGet, routes.users, nil, "details")
//res, err := c.session.Get(u.String(), nil)
if err != nil {
return nil, err
}
var r types.UserListDetailsResponse
res.JSON(&r)
return r.Ocs.Data.Users, nil
}
// User return the details about the specified user
func (c *Client) User(name string) (*types.User, error) {
if name == "" {
@ -77,6 +90,29 @@ func (c *Client) UserCreate(username string, password string, user *types.User)
return c.UserUpdate(user)
}
// UserCreateWithoutPassword create a user without provisioning a password, the email address must be provided to send
// an init password email
func (c *Client) UserCreateWithoutPassword(username, email, displayName string) error {
if c.version.Major < 14 {
return errors.New("unsupported method: requires Nextcloud 14+")
}
if username == "" || email == "" {
return errors.New("username and email cannot be empty")
}
ro := &req.RequestOptions{
Data: map[string]string{
"userid": username,
"email": email,
"displayName": displayName,
},
}
if err := c.userBaseRequest(http.MethodPost, ro); err != nil {
return err
}
return nil
}
//UserDelete delete the user
func (c *Client) UserDelete(name string) error {
return c.userBaseRequest(http.MethodDelete, nil, name)