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

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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  	"fmt"
    24  	"io/ioutil"
    25  	"net/http"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	snap "github.com/snapcore/snapd/cmd/snap"
    30  )
    31  
    32  var mockLoginRsp = `{"type": "sync", "result": {"id":42, "username": "foo", "email": "foo@example.com", "macaroon": "yummy", "discarages":"plenty"}}`
    33  
    34  func makeLoginTestServer(c *C, n *int) func(w http.ResponseWriter, r *http.Request) {
    35  	return func(w http.ResponseWriter, r *http.Request) {
    36  		switch *n {
    37  		case 0:
    38  			c.Check(r.URL.Path, Equals, "/v2/login")
    39  			c.Check(r.Method, Equals, "POST")
    40  			postData, err := ioutil.ReadAll(r.Body)
    41  			c.Assert(err, IsNil)
    42  			c.Check(string(postData), Equals, `{"email":"foo@example.com","password":"some-password"}`+"\n")
    43  			fmt.Fprintln(w, mockLoginRsp)
    44  		default:
    45  			c.Fatalf("unexpected path %q", r.URL.Path)
    46  		}
    47  		*n++
    48  	}
    49  }
    50  
    51  func (s *SnapSuite) TestLoginSimple(c *C) {
    52  	n := 0
    53  	s.RedirectClientToTestServer(makeLoginTestServer(c, &n))
    54  
    55  	// send the password
    56  	s.password = "some-password\n"
    57  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"login", "foo@example.com"})
    58  	c.Assert(err, IsNil)
    59  	c.Assert(rest, DeepEquals, []string{})
    60  	c.Check(s.Stdout(), Equals, `Personal information is handled as per our privacy notice at
    61  https://www.ubuntu.com/legal/dataprivacy/snap-store
    62  
    63  Password of "foo@example.com": 
    64  Login successful
    65  `)
    66  	c.Check(s.Stderr(), Equals, "")
    67  	c.Check(n, Equals, 1)
    68  }
    69  
    70  func (s *SnapSuite) TestLoginAskEmail(c *C) {
    71  	n := 0
    72  	s.RedirectClientToTestServer(makeLoginTestServer(c, &n))
    73  
    74  	// send the email
    75  	fmt.Fprint(s.stdin, "foo@example.com\n")
    76  	// send the password
    77  	s.password = "some-password"
    78  
    79  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"login"})
    80  	c.Assert(err, IsNil)
    81  	c.Assert(rest, DeepEquals, []string{})
    82  	// test slightly ugly, on a real system STDOUT will be:
    83  	//    Email address: foo@example.com\n
    84  	// because the input to stdin is echoed
    85  	c.Check(s.Stdout(), Equals, `Personal information is handled as per our privacy notice at
    86  https://www.ubuntu.com/legal/dataprivacy/snap-store
    87  
    88  Email address: Password of "foo@example.com": 
    89  Login successful
    90  `)
    91  	c.Check(s.Stderr(), Equals, "")
    92  	c.Check(n, Equals, 1)
    93  }