gonextcloud/coverage.html

704 lines
25 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/partitio/gonextcloud/apps.go (0.0%)</option>
<option value="file1">github.com/partitio/gonextcloud/auth.go (100.0%)</option>
<option value="file2">github.com/partitio/gonextcloud/client.go (85.7%)</option>
<option value="file3">github.com/partitio/gonextcloud/groups.go (35.3%)</option>
<option value="file4">github.com/partitio/gonextcloud/users.go (89.0%)</option>
<option value="file5">github.com/partitio/gonextcloud/utils.go (100.0%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">no coverage</span>
<span class="cov1">low coverage</span>
<span class="cov2">*</span>
<span class="cov3">*</span>
<span class="cov4">*</span>
<span class="cov5">*</span>
<span class="cov6">*</span>
<span class="cov7">*</span>
<span class="cov8">*</span>
<span class="cov9">*</span>
<span class="cov10">high coverage</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package gonextcloud
import (
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
"net/http"
)
func (c *Client) AppList() ([]string, error) <span class="cov0" title="0">{
res, err := c.baseRequest(routes.apps, "", "", nil, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">var r types.AppListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Apps, nil</span>
}
func (c *Client) AppListEnabled() ([]string, error) <span class="cov0" title="0">{
ro := &amp;req.RequestOptions{
Params: map[string]string{"filter": "enabled"},
}
res, err := c.baseRequest(routes.apps, "", "", ro, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">var r types.AppListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Apps, nil</span>
}
func (c *Client) AppListDisabled() ([]string, error) <span class="cov0" title="0">{
ro := &amp;req.RequestOptions{
Params: map[string]string{"filter": "disabled"},
}
res, err := c.baseRequest(routes.apps, "", "", ro, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">var r types.AppListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Apps, nil</span>
}
func (c *Client) AppInfos(name string) (types.App, error) <span class="cov0" title="0">{
res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return types.App{}, err
}</span>
<span class="cov0" title="0">var r types.AppResponse
res.JSON(&amp;r)
return r.Ocs.Data, nil</span>
}
func (c *Client) AppEnable(name string) error <span class="cov0" title="0">{
res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodPut)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">var r types.BaseResponse
res.JSON(&amp;r)
return nil</span>
}
func (c *Client) AppDisable(name string) error <span class="cov0" title="0">{
res, err := c.baseRequest(routes.apps, name, "", nil, http.MethodDelete)
if err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov0" title="0">var r types.BaseResponse
res.JSON(&amp;r)
return nil</span>
}
func (c *Client) appsBaseRequest(name string, route string, ro *req.RequestOptions, method string) error <span class="cov0" title="0">{
_, err := c.baseRequest(routes.apps, name, route, ro, method)
return err
}</span>
</pre>
<pre class="file" id="file1" style="display: none">package gonextcloud
import (
"fmt"
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
)
var unauthorized = fmt.Errorf("login first")
func (c *Client) Login(username string, password string) error <span class="cov3" title="3">{
c.username = username
c.password = password
options := req.RequestOptions{
Headers: c.headers,
Auth: []string{c.username, c.password},
}
c.session = req.NewSession(&amp;options)
// TODO What to do with capabilities ? (other thant connection validation)
u := c.baseURL.ResolveReference(routes.capabilities)
res, err := c.session.Get(u.String(), nil)
if err != nil </span><span class="cov1" title="1">{
return err
}</span>
<span class="cov2" title="2">var r types.CapabilitiesResponse
res.JSON(&amp;r)
// No need to check for Ocs.Meta.StatusCode as capabilities are always returned
c.capabilities = &amp;r.Ocs.Data.Capabilities
// Check if authentication failed
if !c.loggedIn() </span><span class="cov1" title="1">{
e := types.APIError{Message: "authentication failed"}
return &amp;e
}</span>
<span class="cov1" title="1">return nil</span>
}
func (c *Client) Logout() error <span class="cov1" title="1">{
c.session.CloseIdleConnections()
c.session.HTTPClient.Jar = nil
// Clear capabilities as it is used to check for valid authentication
c.capabilities = nil
return nil
}</span>
func (c *Client) loggedIn() bool <span class="cov10" title="57">{
// When authentication failed, capabilities doesn't contains core information
if c.capabilities == nil </span><span class="cov1" title="1">{
return false
}</span>
<span class="cov9" title="56">return c.capabilities.Core.WebdavRoot != ""</span>
}
</pre>
<pre class="file" id="file2" style="display: none">/*
Package client is a Go client for the Nextcloud Provisioning API.
For more information about the Provisioning API, see the documentation:
https://docs.nextcloud.com/server/13/admin_manual/configuration_user/user_provisioning_api.html
Usage
You use the library by creating a client object and calling methods on it.
For example, to list all the Nextcloud's instance users:
package main
import (
"fmt"
"github.com/partitio/gonextcloud/client"
)
func main() {
url := "https://www.mynextcloud.com"
username := "admin"
password := "password"
c, err := client.NewClient(url)
if err != nil {
panic(err)
}
if err := c.Login(username, password); err != nil {
panic(err)
}
defer c.Logout()
users, err := c.UserList()
if err != nil {
panic(err)
}
fmt.Println("Users :", users)
}
*/
package gonextcloud
import (
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
"net/url"
)
type Client struct {
baseURL *url.URL
username string
password string
session *req.Session
headers map[string]string
capabilities *types.Capabilities
}
func NewClient(hostname string) (*Client, error) <span class="cov10" title="3">{
baseURL, err := url.ParseRequestURI(hostname)
if err != nil </span><span class="cov6" title="2">{
baseURL, err = url.ParseRequestURI("https://" + hostname)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
}
<span class="cov10" title="3">c := Client{
baseURL: baseURL,
headers: map[string]string{
"OCS-APIREQUEST": "true",
"Accept": "application/json",
},
}
return &amp;c, nil</span>
}
</pre>
<pre class="file" id="file3" style="display: none">package gonextcloud
import (
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
"net/http"
)
func (c *Client) GroupList() ([]string, error) <span class="cov1" title="1">{
res, err := c.baseRequest(routes.groups, "", "", nil, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov1" title="1">var r types.GroupListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Groups, nil</span>
}
func (c *Client) GroupUsers(name string) ([]string, error) <span class="cov0" title="0">{
res, err := c.baseRequest(routes.groups, name, "", nil, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">var r types.UserListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Users, nil</span>
}
func (c *Client) GroupSearch(search string) ([]string, error) <span class="cov0" title="0">{
ro := &amp;req.RequestOptions{
Params: map[string]string{"search": search},
}
res, err := c.baseRequest(routes.groups, "", "", ro, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">var r types.GroupListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Groups, nil</span>
}
func (c *Client) GroupCreate(name string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"groupid": name,
},
}
if err := c.groupBaseRequest("", "", ro, http.MethodPost); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">return nil</span>
}
func (c *Client) GroupDelete(name string) error <span class="cov1" title="1">{
if err := c.groupBaseRequest(name, "", nil, http.MethodDelete); err != nil </span><span class="cov0" title="0">{
return err
}</span>
<span class="cov1" title="1">return nil</span>
}
func (c *Client) GroupSubAdminList(name string) ([]string, error) <span class="cov0" title="0">{
res, err := c.baseRequest(routes.groups, name, "subadmins", nil, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov0" title="0">var r types.UserListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Users, nil</span>
}
func (c *Client) groupBaseRequest(name string, route string, ro *req.RequestOptions, method string) error <span class="cov10" title="2">{
_, err := c.baseRequest(routes.groups, name, route, ro, method)
return err
}</span>
</pre>
<pre class="file" id="file4" style="display: none">package gonextcloud
import (
"encoding/json"
"github.com/fatih/structs"
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
"net/http"
"path"
"strconv"
"strings"
"sync"
)
func (c *Client) UserList() ([]string, error) <span class="cov1" title="1">{
res, err := c.baseRequest(routes.users, "", "", nil, http.MethodGet)
//res, err := c.session.Get(u.String(), nil)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov1" title="1">var r types.UserListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Users, nil</span>
}
func (c *Client) User(name string) (*types.User, error) <span class="cov5" title="13">{
if name == "" </span><span class="cov1" title="1">{
return nil, &amp;types.APIError{Message: "name cannot be empty"}
}</span>
<span class="cov5" title="12">res, err := c.baseRequest(routes.users, name, "", nil, http.MethodGet)
if err != nil </span><span class="cov1" title="1">{
return nil, err
}</span>
<span class="cov5" title="11">var r types.UserResponse
js := res.String()
// Nextcloud does not encode JSON properly
js = reformatJSON(js)
if err := json.Unmarshal([]byte(js), &amp;r); err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov5" title="11">return &amp;r.Ocs.Data, nil</span>
}
func (c *Client) UserSearch(search string) ([]string, error) <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Params: map[string]string{"search": search},
}
res, err := c.baseRequest(routes.users, "", "", ro, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov1" title="1">var r types.UserListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Users, nil</span>
}
func (c *Client) UserCreate(username string, password string, user *types.User) error <span class="cov3" title="4">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"userid": username,
"password": password,
},
}
if err := c.userBaseRequest("", "", ro, http.MethodPost); err != nil </span><span class="cov1" title="1">{
return err
}</span>
<span class="cov2" title="3">if user == nil </span><span class="cov2" title="2">{
return nil
}</span>
<span class="cov1" title="1">return c.UserUpdate(user)</span>
}
func (c *Client) UserDelete(name string) error <span class="cov2" title="3">{
return c.userBaseRequest(name, "", nil, http.MethodDelete)
}</span>
func (c *Client) UserEnable(name string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{},
}
return c.userBaseRequest(name, "enable", ro, http.MethodPut)
}</span>
func (c *Client) UserDisable(name string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{},
}
return c.userBaseRequest(name, "disable", ro, http.MethodPut)
}</span>
func (c *Client) UserSendWelcomeEmail(name string) error <span class="cov0" title="0">{
return c.userBaseRequest(name, "welcome", nil, http.MethodPost)
}</span>
func (c *Client) UserUpdate(user *types.User) error <span class="cov2" title="2">{
m := structs.Map(user)
errs := make(chan types.UpdateError)
var wg sync.WaitGroup
for k := range m </span><span class="cov6" title="22">{
if !ignoredUserField(k) &amp;&amp; m[k].(string) != "" </span><span class="cov5" title="12">{
wg.Add(1)
go func(key string, value string) </span><span class="cov5" title="12">{
defer wg.Done()
if err := c.userUpdateAttribute(user.ID, strings.ToLower(key), value); err != nil </span><span class="cov0" title="0">{
errs &lt;- types.UpdateError{
Field: key,
Error: err,
}
}</span>
}(k, m[k].(string))
}
}
<span class="cov2" title="2">go func() </span><span class="cov2" title="2">{
wg.Wait()
close(errs)
}</span>()
<span class="cov2" title="2">return types.NewUpdateError(errs)</span>
}
func (c *Client) UserUpdateEmail(name string, email string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "email", email)
}</span>
func (c *Client) UserUpdateDisplayName(name string, displayName string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "displayname", displayName)
}</span>
func (c *Client) UserUpdatePhone(name string, phone string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "phone", phone)
}</span>
func (c *Client) UserUpdateAddress(name string, address string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "address", address)
}</span>
func (c *Client) UserUpdateWebSite(name string, website string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "website", website)
}</span>
func (c *Client) UserUpdateTwitter(name string, twitter string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "twitter", twitter)
}</span>
func (c *Client) UserUpdatePassword(name string, password string) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "password", password)
}</span>
func (c *Client) UserUpdateQuota(name string, quota int) error <span class="cov1" title="1">{
return c.userUpdateAttribute(name, "quota", strconv.Itoa(quota))
}</span>
func (c *Client) UserGroupList(name string) ([]string, error) <span class="cov1" title="1">{
res, err := c.baseRequest(routes.users, name, "groups", nil, http.MethodGet)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov1" title="1">var r types.GroupListResponse
res.JSON(&amp;r)
return r.Ocs.Data.Groups, nil</span>
}
func (c *Client) UserGroupAdd(name string, group string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"groupid": group,
},
}
return c.userBaseRequest(name, "groups", ro, http.MethodPost)
}</span>
func (c *Client) UserGroupRemove(name string, group string) error <span class="cov0" title="0">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"groupid": group,
},
}
return c.userBaseRequest(name, "groups", ro, http.MethodDelete)
}</span>
func (c *Client) UserGroupPromote(name string, group string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"groupid": group,
},
}
return c.userBaseRequest(name, "subadmins", ro, http.MethodPost)
}</span>
func (c *Client) UserGroupDemote(name string, group string) error <span class="cov1" title="1">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"groupid": group,
},
}
return c.userBaseRequest(name, "subadmins", ro, http.MethodDelete)
}</span>
func (c *Client) UserGroupSubAdminList(name string) ([]string, error) <span class="cov2" title="2">{
if !c.loggedIn() </span><span class="cov0" title="0">{
return nil, unauthorized
}</span>
<span class="cov2" title="2">u := c.baseURL.ResolveReference(routes.users)
u.Path = path.Join(u.Path, name, "subadmins")
res, err := c.session.Get(u.String(), nil)
if err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
<span class="cov2" title="2">var r types.BaseResponse
res.JSON(&amp;r)
return r.Ocs.Data, nil</span>
}
func (c *Client) userUpdateAttribute(name string, key string, value string) error <span class="cov6" title="20">{
ro := &amp;req.RequestOptions{
Data: map[string]string{
"key": key,
"value": value,
},
}
return c.userBaseRequest(name, "", ro, http.MethodPut)
}</span>
func (c *Client) userBaseRequest(name string, route string, ro *req.RequestOptions, method string) error <span class="cov7" title="32">{
_, err := c.baseRequest(routes.users, name, route, ro, method)
return err
}</span>
func ignoredUserField(key string) bool <span class="cov7" title="44">{
keys := []string{"ID", "Quota", "Enabled", "Groups", "Language"}
for _, k := range keys </span><span class="cov10" title="180">{
if key == k </span><span class="cov6" title="20">{
return true
}</span>
}
<span class="cov6" title="24">return false</span>
}
</pre>
<pre class="file" id="file5" style="display: none">package gonextcloud
import (
"encoding/json"
req "github.com/levigross/grequests"
"github.com/partitio/gonextcloud/types"
"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) <span class="cov10" title="52">{
if !c.loggedIn() </span><span class="cov1" title="1">{
return nil, unauthorized
}</span>
<span class="cov9" title="51">u := c.baseURL.ResolveReference(route)
if name != "" </span><span class="cov9" title="42">{
u.Path = path.Join(u.Path, name)
}</span>
<span class="cov9" title="51">if subroute != "" </span><span class="cov5" title="7">{
u.Path = path.Join(u.Path, subroute)
}</span>
<span class="cov9" title="51">var (
res *req.Response
err error
)
if method == http.MethodGet </span><span class="cov7" title="17">{
res, err = c.session.Get(u.String(), ro)
}</span> else<span class="cov9" title="34"> if method == http.MethodPost </span><span class="cov5" title="7">{
res, err = c.session.Post(u.String(), ro)
}</span> else<span class="cov8" title="27"> if method == http.MethodPut </span><span class="cov8" title="22">{
res, err = c.session.Put(u.String(), ro)
}</span> else<span class="cov4" title="5"> if method == http.MethodDelete </span><span class="cov4" title="5">{
res, err = c.session.Delete(u.String(), ro)
}</span>
<span class="cov9" title="51">if err != nil </span><span class="cov1" title="1">{
return nil, err
}</span>
// As we cannot read the ReaderCloser twice, we use the string content
<span class="cov9" title="50">js := res.String()
var r types.BaseResponse
json.Unmarshal([]byte(js), &amp;r)
if r.Ocs.Meta.Statuscode != 100 </span><span class="cov2" title="2">{
err := types.ErrorFromMeta(r.Ocs.Meta)
return nil, err
}</span>
<span class="cov9" title="48">return res, nil</span>
}
func reformatJSON(json string) string <span class="cov6" title="11">{
// 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
}</span>
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>