github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/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  	options: []*client.CreateUserOptions{{
   146  		Automatic: true,
   147  	}},
   148  	bodies: []string{
   149  		`{"action":"create","automatic":true}`,
   150  	},
   151  	responses: []string{
   152  		// for automatic result can be empty
   153  		`{"type": "sync", "result": []}`,
   154  	},
   155  },
   156  }
   157  
   158  func (cs *clientSuite) TestClientCreateUsers(c *C) {
   159  	for _, test := range createUsersTests {
   160  		cs.reqs = nil
   161  		cs.rsps = test.responses
   162  
   163  		results, err := cs.cli.CreateUsers(test.options)
   164  		if test.error != "" {
   165  			c.Assert(err, ErrorMatches, test.error)
   166  		}
   167  		c.Assert(results, DeepEquals, test.results)
   168  
   169  		var bodies []string
   170  		for _, req := range cs.reqs {
   171  			c.Assert(req.Method, Equals, "POST")
   172  			c.Assert(req.URL.Path, Equals, "/v2/users")
   173  			data, err := ioutil.ReadAll(req.Body)
   174  			c.Assert(err, IsNil)
   175  			bodies = append(bodies, string(data))
   176  		}
   177  
   178  		c.Assert(bodies, DeepEquals, test.bodies)
   179  	}
   180  }
   181  
   182  func (cs *clientSuite) TestClientJSONError(c *C) {
   183  	cs.rsp = `some non-json error message`
   184  	_, err := cs.cli.SysInfo()
   185  	c.Assert(err, ErrorMatches, `cannot obtain system details: cannot decode "some non-json error message": invalid char.*`)
   186  }
   187  
   188  func (cs *clientSuite) TestUsers(c *C) {
   189  	cs.rsp = `{"type": "sync", "result":
   190                       [{"username": "foo","email":"foo@example.com"},
   191                        {"username": "bar","email":"bar@example.com"}]}`
   192  	users, err := cs.cli.Users()
   193  	c.Check(err, IsNil)
   194  	c.Check(users, DeepEquals, []*client.User{
   195  		{Username: "foo", Email: "foo@example.com"},
   196  		{Username: "bar", Email: "bar@example.com"},
   197  	})
   198  }