github.com/w3security/vervet/v5@v5.3.1-0.20230618081846-5bd9b5d799dc/internal/cmd/resource_test.go (about)

     1  package cmd_test
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	qt "github.com/frankban/quicktest"
     9  
    10  	"github.com/w3security/vervet/v5/internal/cmd"
    11  	"github.com/w3security/vervet/v5/testdata"
    12  )
    13  
    14  func cd(c *qt.C, path string) {
    15  	cwd, err := os.Getwd()
    16  	c.Assert(err, qt.IsNil)
    17  	err = os.Chdir(path)
    18  	c.Assert(err, qt.IsNil)
    19  	c.Cleanup(func() {
    20  		err := os.Chdir(cwd)
    21  		c.Assert(err, qt.IsNil)
    22  	})
    23  }
    24  
    25  func TestResourceFiles(t *testing.T) {
    26  	c := qt.New(t)
    27  	tmp := c.TempDir()
    28  	tmpFile := filepath.Join(tmp, "out")
    29  	c.Run("cmd", func(c *qt.C) {
    30  		output, err := os.Create(tmpFile)
    31  		c.Assert(err, qt.IsNil)
    32  		defer output.Close()
    33  		c.Patch(&os.Stdout, output)
    34  		cd(c, testdata.Path("."))
    35  		err = cmd.Vervet.Run([]string{"vervet", "resource", "files"})
    36  		c.Assert(err, qt.IsNil)
    37  	})
    38  	out, err := os.ReadFile(tmpFile)
    39  	c.Assert(err, qt.IsNil)
    40  	c.Assert(string(out), qt.Equals, `
    41  resources/_examples/hello-world/2021-06-01/spec.yaml
    42  resources/_examples/hello-world/2021-06-07/spec.yaml
    43  resources/_examples/hello-world/2021-06-13/spec.yaml
    44  resources/projects/2021-06-04/spec.yaml
    45  resources/projects/2021-08-20/spec.yaml
    46  `[1:])
    47  }
    48  
    49  func TestResourceInfo(t *testing.T) {
    50  	c := qt.New(t)
    51  	tmp := c.TempDir()
    52  	tmpFile := filepath.Join(tmp, "out")
    53  	c.Run("cmd", func(c *qt.C) {
    54  		output, err := os.Create(tmpFile)
    55  		c.Assert(err, qt.IsNil)
    56  		defer output.Close()
    57  		c.Patch(&os.Stdout, output)
    58  		cd(c, testdata.Path("."))
    59  		err = cmd.Vervet.Run([]string{"vervet", "resource", "info"})
    60  		c.Assert(err, qt.IsNil)
    61  	})
    62  	out, err := os.ReadFile(tmpFile)
    63  	c.Assert(err, qt.IsNil)
    64  	c.Assert(string(out), qt.Equals, `
    65  +----------+-------------+-------------------------+--------------------------------------+--------+-------------------+
    66  |   API    |  RESOURCE   |         VERSION         |                 PATH                 | METHOD |     OPERATION     |
    67  +----------+-------------+-------------------------+--------------------------------------+--------+-------------------+
    68  | testdata | hello-world | 2021-06-01~experimental | /examples/hello-world/{id}           | GET    | helloWorldGetOne  |
    69  | testdata | projects    | 2021-06-04~experimental | /orgs/{orgId}/projects               | GET    | getOrgsProjects   |
    70  | testdata | hello-world | 2021-06-07~experimental | /examples/hello-world/{id}           | GET    | helloWorldGetOne  |
    71  | testdata | hello-world | 2021-06-13~beta         | /examples/hello-world                | POST   | helloWorldCreate  |
    72  | testdata | hello-world | 2021-06-13~beta         | /examples/hello-world/{id}           | GET    | helloWorldGetOne  |
    73  | testdata | projects    | 2021-08-20~experimental | /orgs/{org_id}/projects/{project_id} | DELETE | deleteOrgsProject |
    74  +----------+-------------+-------------------------+--------------------------------------+--------+-------------------+
    75  `[1:])
    76  }
    77  
    78  func TestResourceInfoResource(t *testing.T) {
    79  	c := qt.New(t)
    80  	tmp := c.TempDir()
    81  	tmpFile := filepath.Join(tmp, "out")
    82  	c.Run("cmd", func(c *qt.C) {
    83  		output, err := os.Create(tmpFile)
    84  		c.Assert(err, qt.IsNil)
    85  		defer output.Close()
    86  		c.Patch(&os.Stdout, output)
    87  		cd(c, testdata.Path("."))
    88  		err = cmd.Vervet.Run([]string{"vervet", "resource", "info", "testdata", "projects"})
    89  		c.Assert(err, qt.IsNil)
    90  	})
    91  	out, err := os.ReadFile(tmpFile)
    92  	c.Assert(err, qt.IsNil)
    93  	c.Assert(string(out), qt.Equals, `
    94  +----------+----------+-------------------------+--------------------------------------+--------+-------------------+
    95  |   API    | RESOURCE |         VERSION         |                 PATH                 | METHOD |     OPERATION     |
    96  +----------+----------+-------------------------+--------------------------------------+--------+-------------------+
    97  | testdata | projects | 2021-06-04~experimental | /orgs/{orgId}/projects               | GET    | getOrgsProjects   |
    98  | testdata | projects | 2021-08-20~experimental | /orgs/{org_id}/projects/{project_id} | DELETE | deleteOrgsProject |
    99  +----------+----------+-------------------------+--------------------------------------+--------+-------------------+
   100  `[1:])
   101  }