github.com/ubuntu-core/snappy@v0.0.0-20210827154228-9e584df982bb/cmd/snap/cmd_whoami_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  	"net/http"
    24  
    25  	. "gopkg.in/check.v1"
    26  
    27  	snap "github.com/snapcore/snapd/cmd/snap"
    28  	"github.com/snapcore/snapd/osutil"
    29  )
    30  
    31  func (s *SnapSuite) TestWhoamiLoggedInUser(c *C) {
    32  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    33  		panic("unexpected call to snapd API")
    34  	})
    35  
    36  	s.Login(c)
    37  	defer s.Logout(c)
    38  
    39  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"whoami"})
    40  	c.Assert(err, IsNil)
    41  	c.Check(s.Stdout(), Equals, "email: hello@mail.com\n")
    42  }
    43  
    44  func (s *SnapSuite) TestWhoamiNotLoggedInUser(c *C) {
    45  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    46  		panic("unexpected call to snapd API")
    47  	})
    48  
    49  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"whoami"})
    50  	c.Assert(err, IsNil)
    51  	c.Check(s.Stdout(), Equals, "email: -\n")
    52  }
    53  
    54  func (s *SnapSuite) TestWhoamiExtraParamError(c *C) {
    55  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"whoami", "test"})
    56  	c.Check(err, ErrorMatches, "too many arguments for command")
    57  }
    58  
    59  func (s *SnapSuite) TestWhoamiEmptyAuthFile(c *C) {
    60  	s.Login(c)
    61  	defer s.Logout(c)
    62  
    63  	err := osutil.AtomicWriteFile(s.AuthFile, []byte(``), 0600, 0)
    64  	c.Assert(err, IsNil)
    65  
    66  	_, err = snap.Parser(snap.Client()).ParseArgs([]string{"whoami"})
    67  	c.Check(err, ErrorMatches, "EOF")
    68  }