github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/client/users_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2015-2020 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package client_test
    21  
    22  import (
    23  	"io/ioutil"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/client"
    28  )
    29  
    30  func (cs *clientSuite) TestClientRemoveUser(c *C) {
    31  	removed, err := cs.cli.RemoveUser(&client.RemoveUserOptions{})
    32  	c.Assert(err, ErrorMatches, "cannot remove a user without providing a username")
    33  	c.Assert(removed, IsNil)
    34  
    35  	cs.rsp = `{
    36  		"type": "sync",
    37  		"result": {
    38                     "removed": [{"id": 11, "username": "one-user", "email": "user@test.com"}]
    39                  }
    40  	}`
    41  	removed, err = cs.cli.RemoveUser(&client.RemoveUserOptions{Username: "one-user"})
    42  	c.Assert(cs.req.Method, Equals, "POST")
    43  	c.Assert(cs.req.URL.Path, Equals, "/v2/users")
    44  	c.Assert(err, IsNil)
    45  	c.Assert(removed, DeepEquals, []*client.User{
    46  		{ID: 11, Username: "one-user", Email: "user@test.com"},
    47  	})
    48  
    49  	body, err := ioutil.ReadAll(cs.req.Body)
    50  	c.Assert(err, IsNil)
    51  	c.Assert(string(body), Equals, `{"action":"remove","username":"one-user"}`)
    52  }
    53  
    54  func (cs *clientSuite) TestClientRemoveUserError(c *C) {
    55  	removed, err := cs.cli.RemoveUser(nil)
    56  	c.Assert(err, ErrorMatches, "cannot remove a user without providing a username")
    57  	c.Assert(removed, IsNil)
    58  	removed, err = cs.cli.RemoveUser(&client.RemoveUserOptions{})
    59  	c.Assert(err, ErrorMatches, "cannot remove a user without providing a username")
    60  	c.Assert(removed, IsNil)
    61  
    62  	cs.rsp = `{
    63  		"type": "error",
    64  		"result": {"message": "no can do"}
    65  	}`
    66  	removed, err = cs.cli.RemoveUser(&client.RemoveUserOptions{Username: "one-user"})
    67  	c.Assert(cs.req.Method, Equals, "POST")
    68  	c.Assert(cs.req.URL.Path, Equals, "/v2/users")
    69  	c.Assert(err, ErrorMatches, "no can do")
    70  	c.Assert(removed, IsNil)
    71  
    72  	body, err := ioutil.ReadAll(cs.req.Body)
    73  	c.Assert(err, IsNil)
    74  	c.Assert(string(body), Equals, `{"action":"remove","username":"one-user"}`)
    75  }
    76  
    77  func (cs *clientSuite) TestClientCreateUser(c *C) {
    78  	_, err := cs.cli.CreateUser(nil)
    79  	c.Assert(err, ErrorMatches, "cannot create a user without providing an email")
    80  	_, err = cs.cli.CreateUser(&client.CreateUserOptions{})
    81  	c.Assert(err, ErrorMatches, "cannot create a user without providing an email")
    82  
    83  	cs.rsp = `{
    84  		"type": "sync",
    85  		"result": [{
    86                          "username": "karl",
    87                          "ssh-keys": ["one", "two"]
    88  		}]
    89  	}`
    90  	rsp, err := cs.cli.CreateUser(&client.CreateUserOptions{Email: "one@email.com", Sudoer: true, Known: true})
    91  	c.Assert(cs.req.Method, Equals, "POST")
    92  	c.Assert(cs.req.URL.Path, Equals, "/v2/users")
    93  	c.Assert(err, IsNil)
    94  
    95  	body, err := ioutil.ReadAll(cs.req.Body)
    96  	c.Assert(err, IsNil)
    97  	c.Assert(string(body), Equals, `{"action":"create","email":"one@email.com","sudoer":true,"known":true}`)
    98  
    99  	c.Assert(rsp, DeepEquals, &client.CreateUserResult{
   100  		Username: "karl",
   101  		SSHKeys:  []string{"one", "two"},
   102  	})
   103  }
   104  
   105  var createUsersTests = []struct {
   106  	options   []*client.CreateUserOptions
   107  	bodies    []string
   108  	responses []string
   109  	results   []*client.CreateUserResult
   110  	error     string
   111  }{{
   112  	// nothing in -> nothing out
   113  	options: nil,
   114  }, {
   115  	options: []*client.CreateUserOptions{nil},
   116  	error:   "cannot create user from store details without an email to query for",
   117  }, {
   118  	options: []*client.CreateUserOptions{{}},
   119  	error:   "cannot create user from store details without an email to query for",
   120  }, {
   121  	options: []*client.CreateUserOptions{{
   122  		Email:  "one@example.com",
   123  		Sudoer: true,
   124  	}, {
   125  		Known: true,
   126  	}},
   127  	bodies: []string{
   128  		`{"action":"create","email":"one@example.com","sudoer":true}`,
   129  		`{"action":"create","known":true}`,
   130  	},
   131  	responses: []string{
   132  		`{"type": "sync", "result": [{"username": "one", "ssh-keys":["a", "b"]}]}`,
   133  		`{"type": "sync", "result": [{"username": "two"}, {"username": "three"}]}`,
   134  	},
   135  	results: []*client.CreateUserResult{{
   136  		Username: "one",
   137  		SSHKeys:  []string{"a", "b"},
   138  	}, {
   139  		Username: "two",
   140  	}, {
   141  		Username: "three",
   142  	}},
   143  }}
   144  
   145  func (cs *clientSuite) TestClientCreateUsers(c *C) {
   146  	for _, test := range createUsersTests {
   147  		cs.rsps = test.responses
   148  
   149  		results, err := cs.cli.CreateUsers(test.options)
   150  		if test.error != "" {
   151  			c.Assert(err, ErrorMatches, test.error)
   152  		}
   153  		c.Assert(results, DeepEquals, test.results)
   154  
   155  		var bodies []string
   156  		for _, req := range cs.reqs {
   157  			c.Assert(req.Method, Equals, "POST")
   158  			c.Assert(req.URL.Path, Equals, "/v2/users")
   159  			data, err := ioutil.ReadAll(req.Body)
   160  			c.Assert(err, IsNil)
   161  			bodies = append(bodies, string(data))
   162  		}
   163  
   164  		c.Assert(bodies, DeepEquals, test.bodies)
   165  	}
   166  }
   167  
   168  func (cs *clientSuite) TestClientJSONError(c *C) {
   169  	cs.rsp = `some non-json error message`
   170  	_, err := cs.cli.SysInfo()
   171  	c.Assert(err, ErrorMatches, `cannot obtain system details: cannot decode "some non-json error message": invalid char.*`)
   172  }
   173  
   174  func (cs *clientSuite) TestUsers(c *C) {
   175  	cs.rsp = `{"type": "sync", "result":
   176                       [{"username": "foo","email":"foo@example.com"},
   177                        {"username": "bar","email":"bar@example.com"}]}`
   178  	users, err := cs.cli.Users()
   179  	c.Check(err, IsNil)
   180  	c.Check(users, DeepEquals, []*client.User{
   181  		{Username: "foo", Email: "foo@example.com"},
   182  		{Username: "bar", Email: "bar@example.com"},
   183  	})
   184  }