github.com/taubyte/tau-cli@v0.1.13-0.20240326000942-487f0d57edfc/tests/library_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io"
     7  	"testing"
     8  
     9  	"github.com/google/go-github/v53/github"
    10  	commonTest "github.com/taubyte/tau-cli/common/test"
    11  	"github.com/taubyte/tau-cli/constants"
    12  	libraryPrompts "github.com/taubyte/tau-cli/prompts/library"
    13  	"golang.org/x/oauth2"
    14  )
    15  
    16  func basicNewLibrary(name string) []string {
    17  	return []string{
    18  		"new", "-y", "library",
    19  		"--name", name,
    20  		"--description", "some library description",
    21  		"--tags", "tag1, tag2,   tag3",
    22  		"--no-generate-repository",
    23  		"--path", "/",
    24  		"--repository-name", "tb_website_reactdemo",
    25  		"--repository-id", "123456",
    26  		"--no-clone",
    27  		"--branch", "master",
    28  		"--provider", "github",
    29  	}
    30  }
    31  
    32  func TestLibraryAll(t *testing.T) {
    33  	runTests(t, createLibraryMonkey(), true)
    34  }
    35  
    36  func createLibraryMonkey() *testSpider {
    37  	// Define shared variables
    38  	command := "library"
    39  	profileName := "test"
    40  	projectName := "test_project"
    41  	testName := "someLibrary"
    42  	network := "Test"
    43  
    44  	// Create a basic resource of name
    45  	basicNew := func(name string) []string {
    46  		return []string{
    47  			"new", "-y", command,
    48  			"--name", name,
    49  			"--description", "some library description",
    50  			"--tags", "tag1, tag2,   tag3",
    51  			"--no-generate-repository",
    52  			"--path", "/",
    53  			"--repository-name", "tb_website_reactdemo",
    54  			"--no-clone",
    55  			"--branch", "master",
    56  			"--provider", "github",
    57  		}
    58  	}
    59  
    60  	// The config that will be written
    61  	getConfigString := basicValidConfigString(profileName, projectName)
    62  
    63  	// Run before each test
    64  	beforeEach := func(tt testMonkey) [][]string {
    65  		tt.env[constants.CurrentProjectEnvVarName] = projectName
    66  		tt.env[constants.CurrentSelectedNetworkName] = network
    67  		return nil
    68  	}
    69  
    70  	// Define test monkeys that will run in parallel
    71  	tests := []testMonkey{
    72  		{
    73  			name: "Simple new",
    74  			args: []string{
    75  				"new", "-y", command,
    76  				"--name", testName,
    77  				"--description", "some library description",
    78  				"--tags", "tag1, tag2,   tag3",
    79  				"--generate-repository",
    80  				"--private",
    81  				"--template", "empty",
    82  				"--branch", "master",
    83  				"--path", "/",
    84  				"--provider", "github",
    85  				"--no-embed-token",
    86  			},
    87  			cleanUp: func() error {
    88  				ts := oauth2.StaticTokenSource(
    89  					&oauth2.Token{AccessToken: commonTest.GitToken()},
    90  				)
    91  				tc := oauth2.NewClient(context.Background(), ts)
    92  				client := github.NewClient(tc)
    93  
    94  				resp, err := client.Repositories.Delete(context.Background(), commonTest.GitUser, "tb_library_someLibrary")
    95  				if err != nil {
    96  					return fmt.Errorf("req do failed with: %w", err)
    97  				}
    98  				if resp != nil {
    99  					defer resp.Body.Close()
   100  
   101  					if err != nil {
   102  						body, err := io.ReadAll(resp.Body)
   103  						if err != nil {
   104  							return nil
   105  						}
   106  						fmt.Println("Delete repository response", string(body))
   107  					}
   108  				}
   109  
   110  				return nil
   111  			},
   112  		},
   113  		{
   114  			mock: true,
   115  			name: "New from current repository",
   116  			args: []string{
   117  				"query", command, testName,
   118  			},
   119  			wantOut: []string{"tb_website_reactdemo", "github", "master"},
   120  			preRun: [][]string{
   121  				basicNew(testName),
   122  			},
   123  		},
   124  		{
   125  			mock: true,
   126  			name: "edit basic",
   127  			args: []string{
   128  				"query", command, testName,
   129  			},
   130  			wantOut: []string{"/new"},
   131  			preRun: [][]string{
   132  				basicNew(testName),
   133  			},
   134  			children: []testMonkey{
   135  				{
   136  					name: "edit",
   137  					args: []string{
   138  						"edit", "-y", command, testName,
   139  						"--description", "some new library description",
   140  						"--tags", "tag1, tag2,   tag4",
   141  						"--path", "/new",
   142  						"--no-clone",
   143  						"--branch", "master",
   144  						"--domains", "hal.computers.com",
   145  					},
   146  					wantOut: []string{"Edited", "library", testName},
   147  				},
   148  			},
   149  		},
   150  		{
   151  			mock: true,
   152  			name: "delete basic",
   153  			args: []string{
   154  				"query", command, testName,
   155  			},
   156  			exitCode: 1,
   157  			errOut:   []string{fmt.Sprintf(libraryPrompts.NotFound, testName)},
   158  			preRun: [][]string{
   159  				basicNew(testName),
   160  			},
   161  			children: []testMonkey{
   162  				{
   163  					name: "delete",
   164  					args: []string{
   165  						"delete", "-y", command, testName,
   166  					},
   167  				},
   168  			},
   169  		},
   170  		{
   171  			mock: true,
   172  			name: "Query list",
   173  			args: []string{"query", command, "--list"},
   174  			wantOut: []string{
   175  				testName + "1",
   176  				testName + "2",
   177  				// testName + "3", deleted
   178  				testName + "4",
   179  				testName + "5",
   180  			},
   181  			dontWantOut: []string{
   182  				testName + "3",
   183  			},
   184  			preRun: [][]string{
   185  				basicNew(testName + "1"),
   186  				basicNew(testName + "2"),
   187  				basicNew(testName + "3"),
   188  				{"delete", "-y", command, "--name", testName + "3"},
   189  				basicNew(testName + "4"),
   190  				basicNew(testName + "5"),
   191  			},
   192  		},
   193  	}
   194  
   195  	return &testSpider{projectName, tests, beforeEach, getConfigString, "library"}
   196  }