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

     1  package tests
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"net/http"
     7  	"testing"
     8  
     9  	commonTest "github.com/taubyte/tau-cli/common/test"
    10  	"github.com/taubyte/tau-cli/constants"
    11  	websitePrompts "github.com/taubyte/tau-cli/prompts/website"
    12  )
    13  
    14  func TestWebsiteAll(t *testing.T) {
    15  	t.Skip("Mock server needs to be updated")
    16  	runTests(t, createWebsiteMonkey(), true)
    17  }
    18  
    19  func createWebsiteMonkey() *testSpider {
    20  	// Define shared variables
    21  	command := "website"
    22  	profileName := "test"
    23  	projectName := "test_project"
    24  	testName := "someWebsite"
    25  
    26  	testDomain := "test_domain_1"
    27  	testDomainFqdn := "hal.computers.com"
    28  	network := "Test"
    29  
    30  	// Create a basic resource of name
    31  	basicNew := func(name string) []string {
    32  		return []string{
    33  			"new", "-y", command,
    34  			"--name", name,
    35  			"--description", "some website description",
    36  			"--tags", "tag1, tag2,   tag3",
    37  			"--no-generate-repository",
    38  			"--paths", "/",
    39  			"--repository-name", "tb_website_reactdemo",
    40  			"--no-clone",
    41  			"--branch", "master",
    42  			"--provider", "github",
    43  			"--domains", testDomain,
    44  		}
    45  	}
    46  
    47  	// The config that will be written
    48  	getConfigString := basicValidConfigString(profileName, projectName)
    49  
    50  	// Run before each test
    51  	beforeEach := func(tt testMonkey) [][]string {
    52  		tt.env[constants.CurrentProjectEnvVarName] = projectName
    53  		tt.env[constants.CurrentSelectedNetworkName] = network
    54  		return nil
    55  	}
    56  
    57  	// Define test monkeys that will run in parallel
    58  	tests := []testMonkey{
    59  		{
    60  			name: "Simple new",
    61  			args: []string{
    62  				"new", "-y", command,
    63  				"--name", testName,
    64  				"--description", "some website description",
    65  				"--tags", "tag1, tag2,   tag3",
    66  				"--generate-repository",
    67  				"--private",
    68  				"--template", "html",
    69  				"--branch", "master",
    70  				"--paths", "/",
    71  				"--domains", testDomain,
    72  				"--provider", "github",
    73  				"--no-embed-token",
    74  			},
    75  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
    76  			preRun: [][]string{
    77  				basicNewDomain(testDomain, testDomainFqdn),
    78  			},
    79  			cleanUp: func() error {
    80  				// delete https://github.com/taubyte-test/tb_empty_template
    81  				url := fmt.Sprintf("https://api.github.com/repos/%s/tb_website_someWebsite", commonTest.GitUser)
    82  				req, err := http.NewRequest(http.MethodDelete, url, nil)
    83  				if err != nil {
    84  					return err
    85  				}
    86  				req.Header.Add("Accept", "application/vnd.github.v3+json")
    87  				req.Header.Add("Authorization", "token "+commonTest.GitToken())
    88  
    89  				resp, err := http.DefaultClient.Do(req)
    90  				if resp != nil {
    91  					defer resp.Body.Close()
    92  
    93  					if err != nil {
    94  						body, err := io.ReadAll(resp.Body)
    95  						if err != nil {
    96  							return nil
    97  						}
    98  						fmt.Println("Delete repository response", string(body))
    99  					}
   100  				}
   101  				if err != nil {
   102  					return err
   103  				}
   104  
   105  				return nil
   106  			},
   107  		},
   108  		{
   109  			name: "New from current repository",
   110  			args: []string{
   111  				"query", command, testName,
   112  			},
   113  			// TODO confirm values
   114  			preRun: [][]string{
   115  				basicNewDomain(testDomain, testDomainFqdn),
   116  				basicNew(testName),
   117  			},
   118  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   119  		},
   120  		{
   121  			name: "edit basic",
   122  			args: []string{
   123  				"query", command, testName,
   124  			},
   125  			// TODO confirm values
   126  			preRun: [][]string{
   127  				basicNewDomain(testDomain, testDomainFqdn),
   128  				basicNewDomain("test_domain2", "test.computers.com"),
   129  				basicNew(testName),
   130  			},
   131  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   132  			children: []testMonkey{
   133  				{
   134  					name: "edit",
   135  					args: []string{
   136  						"edit", "-y", command, testName,
   137  						"--description", "some new website description",
   138  						"--tags", "tag1, tag2,   tag4",
   139  						"--paths", "/",
   140  						// "--repository-name", "tb_website_reactdemo",
   141  						"--no-clone",
   142  						"--branch", "master",
   143  						"--domains", "test_domain2",
   144  					},
   145  					// TODO confirm values
   146  				},
   147  			},
   148  		},
   149  		{
   150  			name: "delete basic",
   151  			args: []string{
   152  				"query", command, testName,
   153  			},
   154  			exitCode: 1,
   155  			errOut:   []string{fmt.Sprintf(websitePrompts.NotFound, testName)},
   156  			preRun: [][]string{
   157  				basicNewDomain(testDomain, testDomainFqdn),
   158  				basicNew(testName),
   159  			},
   160  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   161  			children: []testMonkey{
   162  				{
   163  					name: "delete",
   164  					args: []string{
   165  						"delete", "-y", command, testName,
   166  					},
   167  				},
   168  			},
   169  		},
   170  		{
   171  			name: "Query list",
   172  			args: []string{"query", command, "--list"},
   173  			wantOut: []string{
   174  				testName + "1",
   175  				testName + "2",
   176  				// testName + "3", deleted
   177  				testName + "4",
   178  				testName + "5",
   179  			},
   180  			dontWantOut: []string{
   181  				testName + "3",
   182  			},
   183  			preRun: [][]string{
   184  				basicNewDomain(testDomain, testDomainFqdn),
   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  			writeFilesInDir: specialWriteFilesInDir(testDomainFqdn),
   193  		},
   194  	}
   195  
   196  	return &testSpider{projectName, tests, beforeEach, getConfigString, "website"}
   197  }