github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/projects/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/identity/v3/projects"
     9  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    11  )
    12  
    13  // ListAvailableOutput provides a single page of available Project results.
    14  const ListAvailableOutput = `
    15  {
    16    "projects": [
    17      {
    18        "description": "my first project",
    19        "domain_id": "11111",
    20        "enabled": true,
    21        "id": "abcde",
    22        "links": {
    23          "self": "http://localhost:5000/identity/v3/projects/abcde"
    24        },
    25        "name": "project 1",
    26        "parent_id": "11111"
    27      },
    28      {
    29        "description": "my second project",
    30        "domain_id": "22222",
    31        "enabled": true,
    32        "id": "bcdef",
    33        "links": {
    34          "self": "http://localhost:5000/identity/v3/projects/bcdef"
    35        },
    36        "name": "project 2",
    37        "parent_id": "22222"
    38      }
    39    ],
    40    "links": {
    41      "next": null,
    42      "previous": null,
    43      "self": "http://localhost:5000/identity/v3/users/foobar/projects"
    44    }
    45  }
    46  `
    47  
    48  // ListOutput provides a single page of Project results.
    49  const ListOutput = `
    50  {
    51    "projects": [
    52      {
    53        "is_domain": false,
    54        "description": "The team that is red",
    55        "domain_id": "default",
    56        "enabled": true,
    57        "id": "1234",
    58        "name": "Red Team",
    59        "parent_id": null,
    60        "tags": ["Red", "Team"],
    61        "test": "old"
    62      },
    63      {
    64        "is_domain": false,
    65        "description": "The team that is blue",
    66        "domain_id": "default",
    67        "enabled": true,
    68        "id": "9876",
    69        "name": "Blue Team",
    70        "parent_id": null,
    71        "options": {
    72              "immutable": true
    73        }
    74      }
    75    ],
    76    "links": {
    77      "next": null,
    78      "previous": null
    79    }
    80  }
    81  `
    82  
    83  // GetOutput provides a Get result.
    84  const GetOutput = `
    85  {
    86    "project": {
    87  		"is_domain": false,
    88  		"description": "The team that is red",
    89  		"domain_id": "default",
    90  		"enabled": true,
    91  		"id": "1234",
    92  		"name": "Red Team",
    93  		"parent_id": null,
    94  		"tags": ["Red", "Team"],
    95  		"test": "old"
    96  	}
    97  }
    98  `
    99  
   100  // CreateRequest provides the input to a Create request.
   101  const CreateRequest = `
   102  {
   103    "project": {
   104  		"description": "The team that is red",
   105  		"name": "Red Team",
   106  		"tags": ["Red", "Team"],
   107  		"test": "old"
   108    }
   109  }
   110  `
   111  
   112  // UpdateRequest provides the input to an Update request.
   113  const UpdateRequest = `
   114  {
   115    "project": {
   116  		"description": "The team that is bright red",
   117  		"name": "Bright Red Team",
   118  		"tags": ["Red"],
   119  		"test": "new"
   120    }
   121  }
   122  `
   123  
   124  // UpdateOutput provides an Update response.
   125  const UpdateOutput = `
   126  {
   127    "project": {
   128  		"is_domain": false,
   129  		"description": "The team that is bright red",
   130  		"domain_id": "default",
   131  		"enabled": true,
   132  		"id": "1234",
   133  		"name": "Bright Red Team",
   134  		"parent_id": null,
   135  		"tags": ["Red"],
   136  		"test": "new"
   137  	}
   138  }
   139  `
   140  
   141  // ListTagsOutput provides the output to a ListTags request.
   142  const ListTagsOutput = `
   143  {
   144      "tags": ["foo", "bar"]
   145  }
   146  `
   147  
   148  // ModifyProjectTagsRequest provides the input to a ModifyTags request.
   149  const ModifyProjectTagsRequest = `
   150  {
   151      "tags": ["foo", "bar"]
   152  }
   153  `
   154  
   155  // ModifyProjectTagsOutput provides the output to a ModifyTags request.
   156  const ModifyProjectTagsOutput = `
   157  {
   158      "links": {
   159          "next": null,
   160          "previous": null,
   161          "self": "http://identity:5000/v3/projects"
   162      },
   163      "projects": [
   164          {
   165              "description": "Test Project",
   166              "domain_id": "default",
   167              "enabled": true,
   168              "id": "3d4c2c82bd5948f0bcab0cf3a7c9b48c",
   169              "links": {
   170                  "self": "http://identity:5000/v3/projects/3d4c2c82bd5948f0bcab0cf3a7c9b48c"
   171              },
   172              "name": "demo",
   173              "tags": ["foo", "bar"]
   174          }
   175      ]
   176  }
   177  `
   178  
   179  // FirstProject is a Project fixture.
   180  var FirstProject = projects.Project{
   181  	Description: "my first project",
   182  	DomainID:    "11111",
   183  	Enabled:     true,
   184  	ID:          "abcde",
   185  	Name:        "project 1",
   186  	ParentID:    "11111",
   187  	Extra: map[string]any{
   188  		"links": map[string]any{"self": "http://localhost:5000/identity/v3/projects/abcde"},
   189  	},
   190  }
   191  
   192  // SecondProject is a Project fixture.
   193  var SecondProject = projects.Project{
   194  	Description: "my second project",
   195  	DomainID:    "22222",
   196  	Enabled:     true,
   197  	ID:          "bcdef",
   198  	Name:        "project 2",
   199  	ParentID:    "22222",
   200  	Extra: map[string]any{
   201  		"links": map[string]any{"self": "http://localhost:5000/identity/v3/projects/bcdef"},
   202  	},
   203  }
   204  
   205  // RedTeam is a Project fixture.
   206  var RedTeam = projects.Project{
   207  	IsDomain:    false,
   208  	Description: "The team that is red",
   209  	DomainID:    "default",
   210  	Enabled:     true,
   211  	ID:          "1234",
   212  	Name:        "Red Team",
   213  	ParentID:    "",
   214  	Tags:        []string{"Red", "Team"},
   215  	Extra:       map[string]any{"test": "old"},
   216  }
   217  
   218  // BlueTeam is a Project fixture.
   219  var BlueTeam = projects.Project{
   220  	IsDomain:    false,
   221  	Description: "The team that is blue",
   222  	DomainID:    "default",
   223  	Enabled:     true,
   224  	ID:          "9876",
   225  	Name:        "Blue Team",
   226  	ParentID:    "",
   227  	Extra:       make(map[string]any),
   228  	Options: map[projects.Option]any{
   229  		projects.Immutable: true,
   230  	},
   231  }
   232  
   233  // UpdatedRedTeam is a Project Fixture.
   234  var UpdatedRedTeam = projects.Project{
   235  	IsDomain:    false,
   236  	Description: "The team that is bright red",
   237  	DomainID:    "default",
   238  	Enabled:     true,
   239  	ID:          "1234",
   240  	Name:        "Bright Red Team",
   241  	ParentID:    "",
   242  	Tags:        []string{"Red"},
   243  	Extra:       map[string]any{"test": "new"},
   244  }
   245  
   246  // ExpectedAvailableProjectsSlice is the slice of projects expected to be returned
   247  // from ListAvailableOutput.
   248  var ExpectedAvailableProjectsSlice = []projects.Project{FirstProject, SecondProject}
   249  
   250  // ExpectedProjectSlice is the slice of projects expected to be returned from ListOutput.
   251  var ExpectedProjectSlice = []projects.Project{RedTeam, BlueTeam}
   252  
   253  var ExpectedTags = projects.Tags{
   254  	Tags: []string{"foo", "bar"},
   255  }
   256  
   257  var ExpectedProjects = projects.ProjectTags{
   258  	Projects: []projects.Project{
   259  		{
   260  			Description: "Test Project",
   261  			DomainID:    "default",
   262  			Enabled:     true,
   263  			ID:          "3d4c2c82bd5948f0bcab0cf3a7c9b48c",
   264  			Extra: map[string]any{"links": map[string]any{
   265  				"self": "http://identity:5000/v3/projects/3d4c2c82bd5948f0bcab0cf3a7c9b48c",
   266  			}},
   267  			Name: "demo",
   268  			Tags: []string{"foo", "bar"},
   269  		},
   270  	},
   271  	Links: map[string]any{
   272  		"next":     nil,
   273  		"previous": nil,
   274  		"self":     "http://identity:5000/v3/projects",
   275  	},
   276  }
   277  
   278  // HandleListAvailableProjectsSuccessfully creates an HTTP handler at `/auth/projects`
   279  // on the test handler mux that responds with a list of two tenants.
   280  func HandleListAvailableProjectsSuccessfully(t *testing.T) {
   281  	th.Mux.HandleFunc("/auth/projects", func(w http.ResponseWriter, r *http.Request) {
   282  		th.TestMethod(t, r, "GET")
   283  		th.TestHeader(t, r, "Accept", "application/json")
   284  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   285  
   286  		w.Header().Set("Content-Type", "application/json")
   287  		w.WriteHeader(http.StatusOK)
   288  		fmt.Fprint(w, ListAvailableOutput)
   289  	})
   290  }
   291  
   292  // HandleListProjectsSuccessfully creates an HTTP handler at `/projects` on the
   293  // test handler mux that responds with a list of two tenants.
   294  func HandleListProjectsSuccessfully(t *testing.T) {
   295  	th.Mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
   296  		th.TestMethod(t, r, "GET")
   297  		th.TestHeader(t, r, "Accept", "application/json")
   298  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   299  
   300  		w.Header().Set("Content-Type", "application/json")
   301  		w.WriteHeader(http.StatusOK)
   302  		fmt.Fprint(w, ListOutput)
   303  	})
   304  }
   305  
   306  // HandleGetProjectSuccessfully creates an HTTP handler at `/projects` on the
   307  // test handler mux that responds with a single project.
   308  func HandleGetProjectSuccessfully(t *testing.T) {
   309  	th.Mux.HandleFunc("/projects/1234", func(w http.ResponseWriter, r *http.Request) {
   310  		th.TestMethod(t, r, "GET")
   311  		th.TestHeader(t, r, "Accept", "application/json")
   312  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   313  
   314  		w.Header().Set("Content-Type", "application/json")
   315  		w.WriteHeader(http.StatusOK)
   316  		fmt.Fprint(w, GetOutput)
   317  	})
   318  }
   319  
   320  // HandleCreateProjectSuccessfully creates an HTTP handler at `/projects` on the
   321  // test handler mux that tests project creation.
   322  func HandleCreateProjectSuccessfully(t *testing.T) {
   323  	th.Mux.HandleFunc("/projects", func(w http.ResponseWriter, r *http.Request) {
   324  		th.TestMethod(t, r, "POST")
   325  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   326  		th.TestJSONRequest(t, r, CreateRequest)
   327  
   328  		w.WriteHeader(http.StatusCreated)
   329  		fmt.Fprint(w, GetOutput)
   330  	})
   331  }
   332  
   333  // HandleDeleteProjectSuccessfully creates an HTTP handler at `/projects` on the
   334  // test handler mux that tests project deletion.
   335  func HandleDeleteProjectSuccessfully(t *testing.T) {
   336  	th.Mux.HandleFunc("/projects/1234", func(w http.ResponseWriter, r *http.Request) {
   337  		th.TestMethod(t, r, "DELETE")
   338  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   339  
   340  		w.WriteHeader(http.StatusNoContent)
   341  	})
   342  }
   343  
   344  // HandleUpdateProjectSuccessfully creates an HTTP handler at `/projects` on the
   345  // test handler mux that tests project updates.
   346  func HandleUpdateProjectSuccessfully(t *testing.T) {
   347  	th.Mux.HandleFunc("/projects/1234", func(w http.ResponseWriter, r *http.Request) {
   348  		th.TestMethod(t, r, "PATCH")
   349  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   350  		th.TestJSONRequest(t, r, UpdateRequest)
   351  
   352  		w.WriteHeader(http.StatusOK)
   353  		fmt.Fprint(w, UpdateOutput)
   354  	})
   355  }
   356  
   357  func HandleListProjectTagsSuccessfully(t *testing.T) {
   358  	th.Mux.HandleFunc("/projects/966b3c7d36a24facaf20b7e458bf2192/tags", func(w http.ResponseWriter, r *http.Request) {
   359  		th.TestMethod(t, r, "GET")
   360  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   361  
   362  		w.WriteHeader(http.StatusOK)
   363  		fmt.Fprint(w, ListTagsOutput)
   364  	})
   365  }
   366  
   367  func HandleModifyProjectTagsSuccessfully(t *testing.T) {
   368  	th.Mux.HandleFunc("/projects/966b3c7d36a24facaf20b7e458bf2192/tags", func(w http.ResponseWriter, r *http.Request) {
   369  		th.TestMethod(t, r, "PUT")
   370  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   371  		th.TestJSONRequest(t, r, ModifyProjectTagsRequest)
   372  
   373  		w.WriteHeader(http.StatusOK)
   374  		fmt.Fprint(w, ModifyProjectTagsOutput)
   375  	})
   376  }
   377  func HandleDeleteProjectTagsSuccessfully(t *testing.T) {
   378  	th.Mux.HandleFunc("/projects/966b3c7d36a24facaf20b7e458bf2192/tags", func(w http.ResponseWriter, r *http.Request) {
   379  		th.TestMethod(t, r, "DELETE")
   380  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   381  
   382  		w.WriteHeader(http.StatusNoContent)
   383  	})
   384  }