github.com/rigado/snapd@v2.42.5-go-mod+incompatible/client/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 client_test 21 22 import ( 23 "fmt" 24 "io/ioutil" 25 "os" 26 "path/filepath" 27 28 "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/client" 31 "github.com/snapcore/snapd/osutil" 32 "github.com/snapcore/snapd/testutil" 33 ) 34 35 func (cs *clientSuite) TestClientLogin(c *check.C) { 36 cs.rsp = `{"type": "sync", "result": 37 {"username": "the-user-name", 38 "macaroon": "the-root-macaroon", 39 "discharges": ["discharge-macaroon"]}}` 40 41 outfile := filepath.Join(c.MkDir(), "json") 42 os.Setenv(client.TestAuthFileEnvKey, outfile) 43 defer os.Unsetenv(client.TestAuthFileEnvKey) 44 45 c.Assert(cs.cli.LoggedInUser(), check.IsNil) 46 47 user, err := cs.cli.Login("username", "pass", "") 48 c.Check(err, check.IsNil) 49 c.Check(user, check.DeepEquals, &client.User{ 50 Username: "the-user-name", 51 Macaroon: "the-root-macaroon", 52 Discharges: []string{"discharge-macaroon"}}) 53 54 c.Assert(cs.cli.LoggedInUser(), check.Not(check.IsNil)) 55 56 c.Check(osutil.FileExists(outfile), check.Equals, true) 57 c.Check(outfile, testutil.FileEquals, `{"username":"the-user-name","macaroon":"the-root-macaroon","discharges":["discharge-macaroon"]}`) 58 } 59 60 func (cs *clientSuite) TestClientLoginWhenLoggedIn(c *check.C) { 61 cs.rsp = `{"type": "sync", "result": 62 {"username": "the-user-name", 63 "email": "zed@bar.com", 64 "macaroon": "the-root-macaroon", 65 "discharges": ["discharge-macaroon"]}}` 66 67 outfile := filepath.Join(c.MkDir(), "json") 68 os.Setenv(client.TestAuthFileEnvKey, outfile) 69 defer os.Unsetenv(client.TestAuthFileEnvKey) 70 71 err := ioutil.WriteFile(outfile, []byte(`{"email":"foo@bar.com","macaroon":"macaroon"}`), 0600) 72 c.Assert(err, check.IsNil) 73 c.Assert(cs.cli.LoggedInUser(), check.DeepEquals, &client.User{ 74 Email: "foo@bar.com", 75 Macaroon: "macaroon", 76 }) 77 78 user, err := cs.cli.Login("username", "pass", "") 79 expected := &client.User{ 80 Email: "zed@bar.com", 81 Username: "the-user-name", 82 Macaroon: "the-root-macaroon", 83 Discharges: []string{"discharge-macaroon"}, 84 } 85 c.Check(err, check.IsNil) 86 c.Check(user, check.DeepEquals, expected) 87 c.Check(cs.req.Header.Get("Authorization"), check.Matches, `Macaroon root="macaroon"`) 88 89 c.Assert(cs.cli.LoggedInUser(), check.DeepEquals, expected) 90 91 c.Check(osutil.FileExists(outfile), check.Equals, true) 92 c.Check(outfile, testutil.FileEquals, `{"username":"the-user-name","email":"zed@bar.com","macaroon":"the-root-macaroon","discharges":["discharge-macaroon"]}`) 93 } 94 95 func (cs *clientSuite) TestClientLoginError(c *check.C) { 96 cs.rsp = `{ 97 "result": {}, 98 "status": "Bad Request", 99 "status-code": 400, 100 "type": "error" 101 }` 102 103 outfile := filepath.Join(c.MkDir(), "json") 104 os.Setenv(client.TestAuthFileEnvKey, outfile) 105 defer os.Unsetenv(client.TestAuthFileEnvKey) 106 107 user, err := cs.cli.Login("username", "pass", "") 108 109 c.Check(user, check.IsNil) 110 c.Check(err, check.NotNil) 111 112 c.Check(osutil.FileExists(outfile), check.Equals, false) 113 } 114 115 func (cs *clientSuite) TestClientLogout(c *check.C) { 116 cs.rsp = `{"type": "sync", "result": {}}` 117 118 outfile := filepath.Join(c.MkDir(), "json") 119 os.Setenv(client.TestAuthFileEnvKey, outfile) 120 defer os.Unsetenv(client.TestAuthFileEnvKey) 121 122 err := ioutil.WriteFile(outfile, []byte(`{"macaroon":"macaroon","discharges":["discharged"]}`), 0600) 123 c.Assert(err, check.IsNil) 124 125 err = cs.cli.Logout() 126 c.Assert(err, check.IsNil) 127 c.Check(cs.req.Method, check.Equals, "POST") 128 c.Check(cs.req.URL.Path, check.Equals, fmt.Sprintf("/v2/logout")) 129 130 c.Check(osutil.FileExists(outfile), check.Equals, false) 131 } 132 133 func (cs *clientSuite) TestWriteAuthData(c *check.C) { 134 outfile := filepath.Join(c.MkDir(), "json") 135 os.Setenv(client.TestAuthFileEnvKey, outfile) 136 defer os.Unsetenv(client.TestAuthFileEnvKey) 137 138 authData := client.User{ 139 Macaroon: "macaroon", 140 Discharges: []string{"discharge"}, 141 } 142 err := client.TestWriteAuth(authData) 143 c.Assert(err, check.IsNil) 144 145 c.Check(osutil.FileExists(outfile), check.Equals, true) 146 c.Check(outfile, testutil.FileEquals, `{"macaroon":"macaroon","discharges":["discharge"]}`) 147 } 148 149 func (cs *clientSuite) TestReadAuthData(c *check.C) { 150 outfile := filepath.Join(c.MkDir(), "json") 151 os.Setenv(client.TestAuthFileEnvKey, outfile) 152 defer os.Unsetenv(client.TestAuthFileEnvKey) 153 154 authData := client.User{ 155 Macaroon: "macaroon", 156 Discharges: []string{"discharge"}, 157 } 158 err := client.TestWriteAuth(authData) 159 c.Assert(err, check.IsNil) 160 161 readUser, err := client.TestReadAuth() 162 c.Assert(err, check.IsNil) 163 c.Check(readUser, check.DeepEquals, &authData) 164 }