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