github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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
    21  
    22  import (
    23  	"fmt"
    24  	"mime/multipart"
    25  	"net/http"
    26  	"strings"
    27  
    28  	"gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/overlord/auth"
    31  	"github.com/snapcore/snapd/testutil"
    32  )
    33  
    34  type apiSuite struct {
    35  	APIBaseSuite
    36  }
    37  
    38  var _ = check.Suite(&apiSuite{})
    39  
    40  func (s *apiSuite) TestUsersOnlyRoot(c *check.C) {
    41  	for _, cmd := range api {
    42  		if strings.Contains(cmd.Path, "user") {
    43  			c.Check(cmd.RootOnly, check.Equals, true, check.Commentf(cmd.Path))
    44  		}
    45  	}
    46  }
    47  
    48  func (s *apiSuite) TestListIncludesAll(c *check.C) {
    49  	// Very basic check to help stop us from not adding all the
    50  	// commands to the command list.
    51  	found := countCommandDecls(c, check.Commentf("TestListIncludesAll"))
    52  
    53  	c.Check(found, check.Equals, len(api),
    54  		check.Commentf(`At a glance it looks like you've not added all the Commands defined in api to the api list.`))
    55  }
    56  
    57  func (s *apiSuite) TestUserFromRequestNoHeader(c *check.C) {
    58  	req, _ := http.NewRequest("GET", "http://example.com", nil)
    59  
    60  	state := snapCmd.d.overlord.State()
    61  	state.Lock()
    62  	user, err := UserFromRequest(state, req)
    63  	state.Unlock()
    64  
    65  	c.Check(err, check.Equals, auth.ErrInvalidAuth)
    66  	c.Check(user, check.IsNil)
    67  }
    68  
    69  func (s *apiSuite) TestUserFromRequestHeaderNoMacaroons(c *check.C) {
    70  	req, _ := http.NewRequest("GET", "http://example.com", nil)
    71  	req.Header.Set("Authorization", "Invalid")
    72  
    73  	state := snapCmd.d.overlord.State()
    74  	state.Lock()
    75  	user, err := UserFromRequest(state, req)
    76  	state.Unlock()
    77  
    78  	c.Check(err, check.ErrorMatches, "authorization header misses Macaroon prefix")
    79  	c.Check(user, check.IsNil)
    80  }
    81  
    82  func (s *apiSuite) TestUserFromRequestHeaderIncomplete(c *check.C) {
    83  	req, _ := http.NewRequest("GET", "http://example.com", nil)
    84  	req.Header.Set("Authorization", `Macaroon root=""`)
    85  
    86  	state := snapCmd.d.overlord.State()
    87  	state.Lock()
    88  	user, err := UserFromRequest(state, req)
    89  	state.Unlock()
    90  
    91  	c.Check(err, check.ErrorMatches, "invalid authorization header")
    92  	c.Check(user, check.IsNil)
    93  }
    94  
    95  func (s *apiSuite) TestUserFromRequestHeaderCorrectMissingUser(c *check.C) {
    96  	req, _ := http.NewRequest("GET", "http://example.com", nil)
    97  	req.Header.Set("Authorization", `Macaroon root="macaroon", discharge="discharge"`)
    98  
    99  	state := snapCmd.d.overlord.State()
   100  	state.Lock()
   101  	user, err := UserFromRequest(state, req)
   102  	state.Unlock()
   103  
   104  	c.Check(err, check.Equals, auth.ErrInvalidAuth)
   105  	c.Check(user, check.IsNil)
   106  }
   107  
   108  func (s *apiSuite) TestUserFromRequestHeaderValidUser(c *check.C) {
   109  	state := snapCmd.d.overlord.State()
   110  	state.Lock()
   111  	expectedUser, err := auth.NewUser(state, "username", "email@test.com", "macaroon", []string{"discharge"})
   112  	state.Unlock()
   113  	c.Check(err, check.IsNil)
   114  
   115  	req, _ := http.NewRequest("GET", "http://example.com", nil)
   116  	req.Header.Set("Authorization", fmt.Sprintf(`Macaroon root="%s"`, expectedUser.Macaroon))
   117  
   118  	state.Lock()
   119  	user, err := UserFromRequest(state, req)
   120  	state.Unlock()
   121  
   122  	c.Check(err, check.IsNil)
   123  	c.Check(user, check.DeepEquals, expectedUser)
   124  }
   125  
   126  func (s *apiSuite) TestIsTrue(c *check.C) {
   127  	form := &multipart.Form{}
   128  	c.Check(isTrue(form, "foo"), check.Equals, false)
   129  	for _, f := range []string{"", "false", "0", "False", "f", "try"} {
   130  		form.Value = map[string][]string{"foo": {f}}
   131  		c.Check(isTrue(form, "foo"), check.Equals, false, check.Commentf("expected %q to be false", f))
   132  	}
   133  	for _, t := range []string{"true", "1", "True", "t"} {
   134  		form.Value = map[string][]string{"foo": {t}}
   135  		c.Check(isTrue(form, "foo"), check.Equals, true, check.Commentf("expected %q to be true", t))
   136  	}
   137  }
   138  
   139  func (s *apiSuite) TestLogsNoServices(c *check.C) {
   140  	// NOTE this is *apiSuite, not *appSuite, so there are no
   141  	// installed snaps with services
   142  
   143  	cmd := testutil.MockCommand(c, "systemctl", "").Also("journalctl", "")
   144  	defer cmd.Restore()
   145  	s.daemon(c)
   146  	s.d.overlord.Loop()
   147  	defer s.d.overlord.Stop()
   148  
   149  	req, err := http.NewRequest("GET", "/v2/logs", nil)
   150  	c.Assert(err, check.IsNil)
   151  
   152  	rsp := getLogs(logsCmd, req, nil).(*resp)
   153  	c.Assert(rsp.Status, check.Equals, 404)
   154  	c.Assert(rsp.Type, check.Equals, ResponseTypeError)
   155  }