github.com/fastly/go-fastly/v5@v5.3.0/fastly/domain_test.go (about)

     1  package fastly
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestClient_Domains(t *testing.T) {
     8  	t.Parallel()
     9  
    10  	var err error
    11  	var tv *Version
    12  	record(t, "domains/version", func(c *Client) {
    13  		tv = testVersion(t, c)
    14  	})
    15  
    16  	// NOTE: Everytime you regenerate the fixtures you'll need to update the
    17  	// domains as they'll potentially be reported as used depending on the
    18  	// service pre-existing.
    19  	domain1 := "integ-test1.go-fastly-1.com"
    20  	domain2 := "integ-test2.go-fastly-2.com"
    21  	domain3 := "integ-test3.go-fastly-3.com"
    22  
    23  	// Create
    24  	var d *Domain
    25  	record(t, "domains/create", func(c *Client) {
    26  		d, err = c.CreateDomain(&CreateDomainInput{
    27  			ServiceID:      testServiceID,
    28  			ServiceVersion: tv.Number,
    29  			Name:           domain1,
    30  			Comment:        "comment",
    31  		})
    32  	})
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	var d2 *Domain
    38  	record(t, "domains/create2", func(c *Client) {
    39  		d2, err = c.CreateDomain(&CreateDomainInput{
    40  			ServiceID:      testServiceID,
    41  			ServiceVersion: tv.Number,
    42  			Name:           domain2,
    43  			Comment:        "comment",
    44  		})
    45  	})
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	// Ensure deleted
    51  	defer func() {
    52  		record(t, "domains/cleanup", func(c *Client) {
    53  			c.DeleteDomain(&DeleteDomainInput{
    54  				ServiceID:      testServiceID,
    55  				ServiceVersion: tv.Number,
    56  				Name:           domain1,
    57  			})
    58  
    59  			c.DeleteDomain(&DeleteDomainInput{
    60  				ServiceID:      testServiceID,
    61  				ServiceVersion: tv.Number,
    62  				Name:           domain3,
    63  			})
    64  		})
    65  	}()
    66  
    67  	if d.Name != domain1 {
    68  		t.Errorf("bad name: %q", d.Name)
    69  	}
    70  	if d.Comment != "comment" {
    71  		t.Errorf("bad comment: %q", d.Comment)
    72  	}
    73  	if d2.Name != domain2 {
    74  		t.Errorf("bad name: %q", d.Name)
    75  	}
    76  
    77  	// List
    78  	var ds []*Domain
    79  	record(t, "domains/list", func(c *Client) {
    80  		ds, err = c.ListDomains(&ListDomainsInput{
    81  			ServiceID:      testServiceID,
    82  			ServiceVersion: tv.Number,
    83  		})
    84  	})
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	if len(ds) < 2 {
    89  		t.Errorf("bad domains: %v", ds)
    90  	}
    91  
    92  	// Get
    93  	var nd *Domain
    94  	record(t, "domains/get", func(c *Client) {
    95  		nd, err = c.GetDomain(&GetDomainInput{
    96  			ServiceID:      testServiceID,
    97  			ServiceVersion: tv.Number,
    98  			Name:           domain1,
    99  		})
   100  	})
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	if d.Name != nd.Name {
   105  		t.Errorf("bad name: %q (%q)", d.Name, nd.Name)
   106  	}
   107  	if d.Comment != nd.Comment {
   108  		t.Errorf("bad comment: %q (%q)", d.Comment, nd.Comment)
   109  	}
   110  
   111  	// Update
   112  	var ud *Domain
   113  	record(t, "domains/update", func(c *Client) {
   114  		ud, err = c.UpdateDomain(&UpdateDomainInput{
   115  			ServiceID:      testServiceID,
   116  			ServiceVersion: tv.Number,
   117  			Name:           domain1,
   118  			NewName:        String(domain3),
   119  		})
   120  	})
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  	if ud.Name != domain3 {
   125  		t.Errorf("bad name: %q", ud.Name)
   126  	}
   127  
   128  	// Validate
   129  	var vd *DomainValidationResult
   130  	record(t, "domains/validation", func(c *Client) {
   131  		vd, err = c.ValidateDomain(&ValidateDomainInput{
   132  			ServiceID:      testServiceID,
   133  			ServiceVersion: tv.Number,
   134  			Name:           domain3,
   135  		})
   136  	})
   137  	if err != nil {
   138  		t.Fatal(err)
   139  	}
   140  	if vd.Valid != false {
   141  		t.Errorf("valid domain unexpected: %q", vd.Metadata.Name)
   142  	}
   143  
   144  	var vds []*DomainValidationResult
   145  	record(t, "domains/validate-all", func(c *Client) {
   146  		vds, err = c.ValidateAllDomains(&ValidateAllDomainsInput{
   147  			ServiceID:      testServiceID,
   148  			ServiceVersion: tv.Number,
   149  		})
   150  	})
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  	if len(vds) < 2 {
   155  		t.Errorf("invalid domains: %v", vds)
   156  	}
   157  	for _, d := range vds {
   158  		if d.Valid != false {
   159  			t.Errorf("valid domain unexpected: %q", d.Metadata.Name)
   160  		}
   161  	}
   162  
   163  	// Delete
   164  	record(t, "domains/delete", func(c *Client) {
   165  		err = c.DeleteDomain(&DeleteDomainInput{
   166  			ServiceID:      testServiceID,
   167  			ServiceVersion: tv.Number,
   168  			Name:           domain3,
   169  		})
   170  	})
   171  	if err != nil {
   172  		t.Fatal(err)
   173  	}
   174  }
   175  
   176  func TestClient_ListDomains_validation(t *testing.T) {
   177  	var err error
   178  	_, err = testClient.ListDomains(&ListDomainsInput{
   179  		ServiceID: "",
   180  	})
   181  	if err != ErrMissingServiceID {
   182  		t.Errorf("bad error: %s", err)
   183  	}
   184  
   185  	_, err = testClient.ListDomains(&ListDomainsInput{
   186  		ServiceID:      "foo",
   187  		ServiceVersion: 0,
   188  	})
   189  	if err != ErrMissingServiceVersion {
   190  		t.Errorf("bad error: %s", err)
   191  	}
   192  }
   193  
   194  func TestClient_CreateDomain_validation(t *testing.T) {
   195  	var err error
   196  	_, err = testClient.CreateDomain(&CreateDomainInput{
   197  		ServiceID: "",
   198  	})
   199  	if err != ErrMissingServiceID {
   200  		t.Errorf("bad error: %s", err)
   201  	}
   202  
   203  	_, err = testClient.CreateDomain(&CreateDomainInput{
   204  		ServiceID:      "foo",
   205  		ServiceVersion: 0,
   206  	})
   207  	if err != ErrMissingServiceVersion {
   208  		t.Errorf("bad error: %s", err)
   209  	}
   210  }
   211  
   212  func TestClient_GetDomain_validation(t *testing.T) {
   213  	var err error
   214  	_, err = testClient.GetDomain(&GetDomainInput{
   215  		ServiceID: "",
   216  	})
   217  	if err != ErrMissingServiceID {
   218  		t.Errorf("bad error: %s", err)
   219  	}
   220  
   221  	_, err = testClient.GetDomain(&GetDomainInput{
   222  		ServiceID:      "foo",
   223  		ServiceVersion: 0,
   224  	})
   225  	if err != ErrMissingServiceVersion {
   226  		t.Errorf("bad error: %s", err)
   227  	}
   228  
   229  	_, err = testClient.GetDomain(&GetDomainInput{
   230  		ServiceID:      "foo",
   231  		ServiceVersion: 1,
   232  		Name:           "",
   233  	})
   234  	if err != ErrMissingName {
   235  		t.Errorf("bad error: %s", err)
   236  	}
   237  }
   238  
   239  func TestClient_UpdateDomain_validation(t *testing.T) {
   240  	var err error
   241  	_, err = testClient.UpdateDomain(&UpdateDomainInput{
   242  		ServiceID: "",
   243  	})
   244  	if err != ErrMissingServiceID {
   245  		t.Errorf("bad error: %s", err)
   246  	}
   247  
   248  	_, err = testClient.UpdateDomain(&UpdateDomainInput{
   249  		ServiceID:      "foo",
   250  		ServiceVersion: 0,
   251  	})
   252  	if err != ErrMissingServiceVersion {
   253  		t.Errorf("bad error: %s", err)
   254  	}
   255  
   256  	_, err = testClient.UpdateDomain(&UpdateDomainInput{
   257  		ServiceID:      "foo",
   258  		ServiceVersion: 1,
   259  		Name:           "",
   260  	})
   261  	if err != ErrMissingName {
   262  		t.Errorf("bad error: %s", err)
   263  	}
   264  
   265  	_, err = testClient.UpdateDomain(&UpdateDomainInput{
   266  		ServiceID:      "foo",
   267  		ServiceVersion: 1,
   268  		Name:           "bar",
   269  	})
   270  	if err != ErrMissingOptionalNameComment {
   271  		t.Errorf("bad error: %s", err)
   272  	}
   273  }
   274  
   275  func TestClient_DeleteDomain_validation(t *testing.T) {
   276  	var err error
   277  	err = testClient.DeleteDomain(&DeleteDomainInput{
   278  		ServiceID: "",
   279  	})
   280  	if err != ErrMissingServiceID {
   281  		t.Errorf("bad error: %s", err)
   282  	}
   283  
   284  	err = testClient.DeleteDomain(&DeleteDomainInput{
   285  		ServiceID:      "foo",
   286  		ServiceVersion: 0,
   287  	})
   288  	if err != ErrMissingServiceVersion {
   289  		t.Errorf("bad error: %s", err)
   290  	}
   291  
   292  	err = testClient.DeleteDomain(&DeleteDomainInput{
   293  		ServiceID:      "foo",
   294  		ServiceVersion: 1,
   295  		Name:           "",
   296  	})
   297  	if err != ErrMissingName {
   298  		t.Errorf("bad error: %s", err)
   299  	}
   300  }
   301  
   302  func TestClient_ValidateDomain_validation(t *testing.T) {
   303  	var err error
   304  	_, err = testClient.ValidateDomain(&ValidateDomainInput{
   305  		ServiceID: "",
   306  	})
   307  	if err != ErrMissingServiceID {
   308  		t.Errorf("bad error: %s", err)
   309  	}
   310  
   311  	_, err = testClient.ValidateDomain(&ValidateDomainInput{
   312  		ServiceID:      "foo",
   313  		ServiceVersion: 0,
   314  	})
   315  	if err != ErrMissingServiceVersion {
   316  		t.Errorf("bad error: %s", err)
   317  	}
   318  
   319  	_, err = testClient.ValidateDomain(&ValidateDomainInput{
   320  		ServiceID:      "foo",
   321  		ServiceVersion: 1,
   322  		Name:           "",
   323  	})
   324  	if err != ErrMissingName {
   325  		t.Errorf("bad error: %s", err)
   326  	}
   327  }