• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-cli / 1779499080

22 Apr 2025 10:11AM UTC coverage: 1.737% (-30.1%) from 31.802%
1779499080

push

gitlab-ci

web-flow
Merge pull request #277 from alfrunes/MEN-7794

MEN-7794: Add support for pagination when listing devices

28 of 82 new or added lines in 4 files covered. (34.15%)

770 existing lines in 17 files now uncovered.

45 of 2590 relevant lines covered (1.74%)

0.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/client/useradm/client.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package useradm
15

16
import (
17
        "bytes"
18
        "context"
19
        "fmt"
20
        "io"
21
        "net/http"
22
        "net/http/httputil"
23
        "time"
24

25
        "github.com/pkg/errors"
26

27
        "github.com/mendersoftware/mender-cli/client"
28
        "github.com/mendersoftware/mender-cli/log"
29
)
30

31
const (
32
        loginUrl = "/api/management/v1/useradm/auth/login"
33
        timeout  = 10 * time.Second
34
)
35

36
type Client struct {
37
        url      string
38
        loginUrl string
39
        client   *http.Client
40
}
41

UNCOV
42
func NewClient(url string, skipVerify bool) *Client {
×
UNCOV
43
        return &Client{
×
UNCOV
44
                url:      url,
×
UNCOV
45
                loginUrl: client.JoinURL(url, loginUrl),
×
UNCOV
46
                client:   client.NewHttpClient(skipVerify),
×
UNCOV
47
        }
×
UNCOV
48
}
×
49

UNCOV
50
func (c *Client) Login(user, pass string, token string) ([]byte, error) {
×
UNCOV
51
        var reader *bytes.Reader
×
UNCOV
52
        var req *http.Request
×
UNCOV
53
        var err error
×
UNCOV
54

×
UNCOV
55
        if len(token) > 1 {
×
56
                tokenJson := "{\"token2fa\":\"" + token + "\"}"
×
57
                reader = bytes.NewReader([]byte(tokenJson))
×
58
                req, err = http.NewRequest(http.MethodPost, c.loginUrl, reader)
×
UNCOV
59
        } else {
×
UNCOV
60
                req, err = http.NewRequest(http.MethodPost, c.loginUrl, nil)
×
UNCOV
61
        }
×
UNCOV
62
        if err != nil {
×
63
                return nil, err
×
64
        }
×
65

UNCOV
66
        ctx, cancel := context.WithTimeout(context.Background(), timeout)
×
UNCOV
67
        defer cancel()
×
UNCOV
68

×
UNCOV
69
        req.SetBasicAuth(user, pass)
×
UNCOV
70
        req.Header.Set("Content-Type", "application/json")
×
UNCOV
71

×
UNCOV
72
        reqDump, _ := httputil.DumpRequest(req, true)
×
UNCOV
73
        log.Verbf("sending request: \n%v", string(reqDump))
×
UNCOV
74

×
UNCOV
75
        rsp, err := c.client.Do(req.WithContext(ctx))
×
UNCOV
76
        if err != nil {
×
77
                return nil, errors.Wrap(err, "POST /auth/login request failed")
×
78
        }
×
UNCOV
79
        defer rsp.Body.Close()
×
UNCOV
80

×
UNCOV
81
        rspDump, _ := httputil.DumpResponse(rsp, true)
×
UNCOV
82
        log.Verbf("response: \n%v\n", string(rspDump))
×
UNCOV
83

×
UNCOV
84
        body, err := io.ReadAll(rsp.Body)
×
UNCOV
85
        if err != nil {
×
86
                return nil, errors.Wrap(err, "can't read request body")
×
87
        }
×
88

UNCOV
89
        if rsp.StatusCode != http.StatusOK {
×
90
                return nil, errors.New(fmt.Sprintf("login failed with status %d", rsp.StatusCode))
×
91
        }
×
92

UNCOV
93
        return body, nil
×
94
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc