github.com/tompreston/snapd@v0.0.0-20210817193607-954edfcb9611/daemon/api_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2020 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 daemon_test
    21  
    22  import (
    23  	"fmt"
    24  	"mime/multipart"
    25  	"net/http"
    26  
    27  	"gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/daemon"
    30  	"github.com/snapcore/snapd/overlord/auth"
    31  	"github.com/snapcore/snapd/overlord/state"
    32  )
    33  
    34  type apiSuite struct {
    35  	st *state.State
    36  }
    37  
    38  var _ = check.Suite(&apiSuite{})
    39  
    40  func (s *apiSuite) SetUpTest(c *check.C) {
    41  	s.st = state.New(nil)
    42  }
    43  
    44  func (s *apiSuite) TestListIncludesAll(c *check.C) {
    45  	// Very basic check to help stop us from not adding all the
    46  	// commands to the command list.
    47  	found := countCommandDecls(c, check.Commentf("TestListIncludesAll"))
    48  
    49  	c.Check(found, check.Equals, len(daemon.APICommands()),
    50  		check.Commentf(`At a glance it looks like you've not added all the Commands defined in api to the api list.`))
    51  }
    52  
    53  func (s *apiSuite) TestserFromRequestNoHeader(c *check.C) {
    54  	req, _ := http.NewRequest("GET", "http://example.com", nil)
    55  
    56  	s.st.Lock()
    57  	user, err := daemon.UserFromRequest(s.st, req)
    58  	s.st.Unlock()
    59  
    60  	c.Check(err, check.Equals, auth.ErrInvalidAuth)
    61  	c.Check(user, check.IsNil)
    62  }
    63  
    64  func (s *apiSuite) TestUserFromRequestHeaderNoMacaroons(c *check.C) {
    65  	req, _ := http.NewRequest("GET", "http://example.com", nil)
    66  	req.Header.Set("Authorization", "Invalid")
    67  
    68  	s.st.Lock()
    69  	user, err := daemon.UserFromRequest(s.st, req)
    70  	s.st.Unlock()
    71  
    72  	c.Check(err, check.ErrorMatches, "authorization header misses Macaroon prefix")
    73  	c.Check(user, check.IsNil)
    74  }
    75  
    76  func (s *apiSuite) TestUserFromRequestHeaderIncomplete(c *check.C) {
    77  	req, _ := http.NewRequest("GET", "http://example.com", nil)
    78  	req.Header.Set("Authorization", `Macaroon root=""`)
    79  
    80  	s.st.Lock()
    81  	user, err := daemon.UserFromRequest(s.st, req)
    82  	s.st.Unlock()
    83  
    84  	c.Check(err, check.ErrorMatches, "invalid authorization header")
    85  	c.Check(user, check.IsNil)
    86  }
    87  
    88  func (s *apiSuite) TestUserFromRequestHeaderCorrectMissingUser(c *check.C) {
    89  	req, _ := http.NewRequest("GET", "http://example.com", nil)
    90  	req.Header.Set("Authorization", `Macaroon root="macaroon", discharge="discharge"`)
    91  
    92  	s.st.Lock()
    93  	user, err := daemon.UserFromRequest(s.st, req)
    94  	s.st.Unlock()
    95  
    96  	c.Check(err, check.Equals, auth.ErrInvalidAuth)
    97  	c.Check(user, check.IsNil)
    98  }
    99  
   100  func (s *apiSuite) TestUserFromRequestHeaderValidUser(c *check.C) {
   101  	s.st.Lock()
   102  	expectedUser, err := auth.NewUser(s.st, "username", "email@test.com", "macaroon", []string{"discharge"})
   103  	s.st.Unlock()
   104  	c.Check(err, check.IsNil)
   105  
   106  	req, _ := http.NewRequest("GET", "http://example.com", nil)
   107  	req.Header.Set("Authorization", fmt.Sprintf(`Macaroon root="%s"`, expectedUser.Macaroon))
   108  
   109  	s.st.Lock()
   110  	user, err := daemon.UserFromRequest(s.st, req)
   111  	s.st.Unlock()
   112  
   113  	c.Check(err, check.IsNil)
   114  	c.Check(user, check.DeepEquals, expectedUser)
   115  }
   116  
   117  func (s *apiSuite) TestIsTrue(c *check.C) {
   118  	form := &multipart.Form{}
   119  	c.Check(daemon.IsTrue(form, "foo"), check.Equals, false)
   120  	for _, f := range []string{"", "false", "0", "False", "f", "try"} {
   121  		form.Value = map[string][]string{"foo": {f}}
   122  		c.Check(daemon.IsTrue(form, "foo"), check.Equals, false, check.Commentf("expected %q to be false", f))
   123  	}
   124  	for _, t := range []string{"true", "1", "True", "t"} {
   125  		form.Value = map[string][]string{"foo": {t}}
   126  		c.Check(daemon.IsTrue(form, "foo"), check.Equals, true, check.Commentf("expected %q to be true", t))
   127  	}
   128  }