github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap/cmd_remove_user_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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 main_test
    21  
    22  import (
    23  	"encoding/json"
    24  	"fmt"
    25  	"net/http"
    26  
    27  	"gopkg.in/check.v1"
    28  
    29  	snap "github.com/snapcore/snapd/cmd/snap"
    30  )
    31  
    32  var removeUserJsonFmtReplyHappy = `{
    33    "type": "sync",
    34    "result": {
    35      "removed": [{"username": %q}]
    36    }
    37  }`
    38  
    39  var removeUserJsonReplyTooMany = `{
    40    "type": "sync",
    41    "result": {
    42      "removed": [{"username": "too"}, {"username": "many"}]
    43    }
    44  }`
    45  
    46  var removeUserJsonReplyTooFew = `{
    47    "type": "sync",
    48    "result": {
    49      "removed": []
    50    }
    51  }`
    52  
    53  func makeRemoveUserChecker(c *check.C, n *int, username string, fmtJsonReply string) func(w http.ResponseWriter, r *http.Request) {
    54  	f := func(w http.ResponseWriter, r *http.Request) {
    55  		switch *n {
    56  		case 0:
    57  			c.Check(r.Method, check.Equals, "POST")
    58  			c.Check(r.URL.Path, check.Equals, "/v2/users")
    59  			var gotBody map[string]interface{}
    60  			dec := json.NewDecoder(r.Body)
    61  			err := dec.Decode(&gotBody)
    62  			c.Assert(err, check.IsNil)
    63  
    64  			wantBody := map[string]interface{}{
    65  				"username": username,
    66  				"action":   "remove",
    67  			}
    68  			c.Check(gotBody, check.DeepEquals, wantBody)
    69  
    70  			fmt.Fprint(w, fmtJsonReply)
    71  		default:
    72  			c.Fatalf("got too many requests (now on %d)", *n+1)
    73  		}
    74  
    75  		*n++
    76  	}
    77  	return f
    78  }
    79  
    80  func (s *SnapSuite) TestRemoveUser(c *check.C) {
    81  	n := 0
    82  	username := "karl"
    83  	s.RedirectClientToTestServer(makeRemoveUserChecker(c, &n, username, fmt.Sprintf(removeUserJsonFmtReplyHappy, username)))
    84  
    85  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"remove-user", "karl"})
    86  	c.Assert(err, check.IsNil)
    87  	c.Check(rest, check.DeepEquals, []string{})
    88  	c.Check(n, check.Equals, 1)
    89  	c.Assert(s.Stdout(), check.Equals, fmt.Sprintf("removed user %q\n", username))
    90  	c.Assert(s.Stderr(), check.Equals, "")
    91  }
    92  
    93  func (s *SnapSuite) TestRemoveUserUnhappyTooMany(c *check.C) {
    94  	n := 0
    95  	s.RedirectClientToTestServer(makeRemoveUserChecker(c, &n, "karl", removeUserJsonReplyTooMany))
    96  
    97  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"remove-user", "karl"})
    98  	c.Assert(err, check.ErrorMatches, `internal error: RemoveUser returned unexpected number of removed users: 2`)
    99  	c.Check(n, check.Equals, 1)
   100  }
   101  
   102  func (s *SnapSuite) TestRemoveUserUnhappyTooFew(c *check.C) {
   103  	n := 0
   104  	s.RedirectClientToTestServer(makeRemoveUserChecker(c, &n, "karl", removeUserJsonReplyTooFew))
   105  
   106  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"remove-user", "karl"})
   107  	c.Assert(err, check.ErrorMatches, `internal error: RemoveUser returned unexpected number of removed users: 0`)
   108  	c.Check(n, check.Equals, 1)
   109  }