github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/client/cohort_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 client_test 21 22 import ( 23 "encoding/json" 24 "errors" 25 "io/ioutil" 26 27 "golang.org/x/xerrors" 28 29 "gopkg.in/check.v1" 30 ) 31 32 func (cs *clientSuite) TestClientCreateCohortsEndpoint(c *check.C) { 33 cs.cli.CreateCohorts([]string{"foo", "bar"}) 34 c.Check(cs.req.Method, check.Equals, "POST") 35 c.Check(cs.req.URL.Path, check.Equals, "/v2/cohorts") 36 37 body, err := ioutil.ReadAll(cs.req.Body) 38 c.Assert(err, check.IsNil) 39 var jsonBody map[string]interface{} 40 err = json.Unmarshal(body, &jsonBody) 41 c.Assert(err, check.IsNil) 42 c.Check(jsonBody, check.DeepEquals, map[string]interface{}{ 43 "action": "create", 44 "snaps": []interface{}{"foo", "bar"}, 45 }) 46 } 47 48 func (cs *clientSuite) TestClientCreateCohorts(c *check.C) { 49 cs.rsp = `{ 50 "type": "sync", 51 "status-code": 200, 52 "result": {"foo": "xyzzy", "bar": "what-what"} 53 }` 54 cohorts, err := cs.cli.CreateCohorts([]string{"foo", "bar"}) 55 c.Assert(err, check.IsNil) 56 c.Check(cohorts, check.DeepEquals, map[string]string{ 57 "foo": "xyzzy", 58 "bar": "what-what", 59 }) 60 61 body, err := ioutil.ReadAll(cs.req.Body) 62 c.Assert(err, check.IsNil) 63 var jsonBody map[string]interface{} 64 err = json.Unmarshal(body, &jsonBody) 65 c.Assert(err, check.IsNil) 66 c.Check(jsonBody, check.DeepEquals, map[string]interface{}{ 67 "action": "create", 68 "snaps": []interface{}{"foo", "bar"}, 69 }) 70 } 71 72 func (cs *clientSuite) TestClientCreateCohortsErrIsWrapped(c *check.C) { 73 cs.err = errors.New("boom") 74 _, err := cs.cli.CreateCohorts([]string{"foo", "bar"}) 75 var e xerrors.Wrapper 76 c.Assert(err, check.Implements, &e) 77 }