github.com/rigado/snapd@v2.42.5-go-mod+incompatible/usersession/agent/rest_api_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 agent_test
    21  
    22  import (
    23  	"encoding/json"
    24  	"fmt"
    25  	"net/http/httptest"
    26  	"os"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	"github.com/snapcore/snapd/dirs"
    31  	"github.com/snapcore/snapd/usersession/agent"
    32  )
    33  
    34  type restSuite struct{}
    35  
    36  var _ = Suite(&restSuite{})
    37  
    38  func (s *restSuite) SetUpTest(c *C) {
    39  	dirs.SetRootDir(c.MkDir())
    40  	xdgRuntimeDir := fmt.Sprintf("%s/%d", dirs.XdgRuntimeDirBase, os.Getuid())
    41  	c.Assert(os.MkdirAll(xdgRuntimeDir, 0700), IsNil)
    42  }
    43  
    44  func (s *restSuite) TearDownTest(c *C) {
    45  	dirs.SetRootDir("")
    46  }
    47  
    48  type resp struct {
    49  	Type   agent.ResponseType `json:"type"`
    50  	Result interface{}        `json:"result"`
    51  }
    52  
    53  func (s *restSuite) TestSessionInfo(c *C) {
    54  	// the agent.SessionInfo end point only supports GET requests
    55  	c.Check(agent.SessionInfoCmd.PUT, IsNil)
    56  	c.Check(agent.SessionInfoCmd.POST, IsNil)
    57  	c.Check(agent.SessionInfoCmd.DELETE, IsNil)
    58  	c.Assert(agent.SessionInfoCmd.GET, NotNil)
    59  
    60  	c.Check(agent.SessionInfoCmd.Path, Equals, "/v1/session-info")
    61  
    62  	a, err := agent.New()
    63  	c.Assert(err, IsNil)
    64  	a.Version = "42b1"
    65  	rec := httptest.NewRecorder()
    66  	agent.SessionInfoCmd.GET(agent.SessionInfoCmd, nil).ServeHTTP(rec, nil)
    67  	c.Check(rec.Code, Equals, 200)
    68  	c.Check(rec.HeaderMap.Get("Content-Type"), Equals, "application/json")
    69  
    70  	var rsp resp
    71  	c.Assert(json.Unmarshal(rec.Body.Bytes(), &rsp), IsNil)
    72  	c.Check(rsp.Type, Equals, agent.ResponseTypeSync)
    73  	c.Check(rsp.Result, DeepEquals, map[string]interface{}{
    74  		"version": "42b1",
    75  	})
    76  }