github.com/aiven/aiven-go-client@v1.36.0/elasticsearch_acls_test.go (about)

     1  package aiven
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"reflect"
     8  	"testing"
     9  )
    10  
    11  func setupElasticsearchACLsTestCase(t *testing.T) (*Client, func(t *testing.T)) {
    12  	t.Log("setup ElasticsearchACLs test case")
    13  
    14  	const (
    15  		UserName     = "test@aiven.io"
    16  		UserPassword = "testabcd"
    17  		AccessToken  = "some-random-token"
    18  	)
    19  
    20  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    21  		if r.URL.Path == "/userauth" {
    22  			w.Header().Set("Content-Type", "application/json")
    23  			w.WriteHeader(http.StatusOK)
    24  			err := json.NewEncoder(w).Encode(authResponse{
    25  				Token: AccessToken,
    26  				State: "active",
    27  			})
    28  
    29  			if err != nil {
    30  				t.Error(err)
    31  			}
    32  			return
    33  		}
    34  
    35  		if r.URL.Path == "/project/test-pr/service/test-sr/elasticsearch/acl" {
    36  			w.Header().Set("Content-Type", "application/json")
    37  			w.WriteHeader(http.StatusOK)
    38  			err := json.NewEncoder(w).Encode(ElasticSearchACLResponse{
    39  				ElasticSearchACLConfig: ElasticSearchACLConfig{
    40  					ACLs: []ElasticSearchACL{
    41  						{
    42  							Rules: []ElasticsearchACLRule{{
    43  								Index:      "_all",
    44  								Permission: "admin",
    45  							}},
    46  							Username: "test-user",
    47  						},
    48  					},
    49  					Enabled:     true,
    50  					ExtendedAcl: false,
    51  				}})
    52  
    53  			if err != nil {
    54  				t.Error(err)
    55  			}
    56  
    57  			return
    58  		}
    59  	}))
    60  
    61  	apiUrl = ts.URL
    62  
    63  	c, err := NewUserClient(UserName, UserPassword, "aiven-go-client-test/"+Version())
    64  	if err != nil {
    65  		t.Fatalf("user authentication error: %s", err)
    66  	}
    67  
    68  	return c, func(t *testing.T) {
    69  		t.Log("teardown ElasticsearchACLs test case")
    70  		ts.Close()
    71  	}
    72  }
    73  
    74  func TestElasticSearchACLsHandler_Update(t *testing.T) {
    75  	c, tearDown := setupElasticsearchACLsTestCase(t)
    76  	defer tearDown(t)
    77  
    78  	type fields struct {
    79  		client *Client
    80  	}
    81  	type args struct {
    82  		project string
    83  		service string
    84  		req     ElasticsearchACLRequest
    85  	}
    86  	tests := []struct {
    87  		name    string
    88  		fields  fields
    89  		args    args
    90  		want    *ElasticSearchACLResponse
    91  		wantErr bool
    92  	}{
    93  		{
    94  			"correct",
    95  			fields{client: c},
    96  			args{
    97  				project: "test-pr",
    98  				service: "test-sr",
    99  				req: ElasticsearchACLRequest{
   100  					ElasticSearchACLConfig: ElasticSearchACLConfig{
   101  						ACLs: []ElasticSearchACL{
   102  							{
   103  								Rules: []ElasticsearchACLRule{{
   104  									Index:      "_all",
   105  									Permission: "admin",
   106  								}},
   107  								Username: "test-user",
   108  							},
   109  						},
   110  						Enabled:     true,
   111  						ExtendedAcl: false,
   112  					},
   113  				},
   114  			},
   115  			&ElasticSearchACLResponse{
   116  				ElasticSearchACLConfig: ElasticSearchACLConfig{
   117  					ACLs: []ElasticSearchACL{
   118  						{
   119  							Rules: []ElasticsearchACLRule{{
   120  								Index:      "_all",
   121  								Permission: "admin",
   122  							}},
   123  							Username: "test-user",
   124  						},
   125  					},
   126  					Enabled:     true,
   127  					ExtendedAcl: false,
   128  				},
   129  			},
   130  			false,
   131  		},
   132  	}
   133  	for _, tt := range tests {
   134  		t.Run(tt.name, func(t *testing.T) {
   135  			h := &ElasticSearchACLsHandler{
   136  				client: tt.fields.client,
   137  			}
   138  			got, err := h.Update(tt.args.project, tt.args.service, tt.args.req)
   139  			if (err != nil) != tt.wantErr {
   140  				t.Errorf("Update() error = %v, wantErr %v", err, tt.wantErr)
   141  				return
   142  			}
   143  			if !reflect.DeepEqual(got, tt.want) {
   144  				t.Errorf("Update() got = %v, want %v", got, tt.want)
   145  			}
   146  		})
   147  	}
   148  }
   149  
   150  func TestElasticSearchACLsHandler_Get(t *testing.T) {
   151  	c, tearDown := setupElasticsearchACLsTestCase(t)
   152  	defer tearDown(t)
   153  
   154  	type fields struct {
   155  		client *Client
   156  	}
   157  	type args struct {
   158  		project string
   159  		service string
   160  	}
   161  	tests := []struct {
   162  		name    string
   163  		fields  fields
   164  		args    args
   165  		want    *ElasticSearchACLResponse
   166  		wantErr bool
   167  	}{
   168  		{
   169  			"correct",
   170  			fields{client: c},
   171  			args{
   172  				project: "test-pr",
   173  				service: "test-sr",
   174  			},
   175  			&ElasticSearchACLResponse{
   176  				ElasticSearchACLConfig: ElasticSearchACLConfig{
   177  					ACLs: []ElasticSearchACL{
   178  						{
   179  							Rules: []ElasticsearchACLRule{{
   180  								Index:      "_all",
   181  								Permission: "admin",
   182  							}},
   183  							Username: "test-user",
   184  						},
   185  					},
   186  					Enabled:     true,
   187  					ExtendedAcl: false,
   188  				},
   189  				APIResponse: APIResponse{},
   190  			},
   191  			false,
   192  		},
   193  	}
   194  	for _, tt := range tests {
   195  		t.Run(tt.name, func(t *testing.T) {
   196  			h := &ElasticSearchACLsHandler{
   197  				client: tt.fields.client,
   198  			}
   199  			got, err := h.Get(tt.args.project, tt.args.service)
   200  			if (err != nil) != tt.wantErr {
   201  				t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr)
   202  				return
   203  			}
   204  			if !reflect.DeepEqual(got, tt.want) {
   205  				t.Errorf("List() got = %v, want %v", got, tt.want)
   206  			}
   207  		})
   208  	}
   209  }
   210  
   211  func TestElasticSearchACLConfig_Add(t *testing.T) {
   212  	type fields struct {
   213  		ACLs        []ElasticSearchACL
   214  		Enabled     bool
   215  		ExtendedAcl bool
   216  	}
   217  	type args struct {
   218  		acl ElasticSearchACL
   219  	}
   220  	tests := []struct {
   221  		name   string
   222  		fields fields
   223  		args   args
   224  		want   *ElasticSearchACLConfig
   225  	}{
   226  		{
   227  			"add-multiple",
   228  			fields{
   229  				ACLs: []ElasticSearchACL{
   230  					{
   231  						Username: "test-user",
   232  						Rules: []ElasticsearchACLRule{
   233  							{
   234  								Index:      "_rw",
   235  								Permission: "write",
   236  							},
   237  						}},
   238  					{
   239  						Username: "test-user2",
   240  						Rules: []ElasticsearchACLRule{
   241  							{
   242  								Index:      "_all",
   243  								Permission: "admin",
   244  							},
   245  						}},
   246  				},
   247  			},
   248  			args{acl: ElasticSearchACL{
   249  				Rules: []ElasticsearchACLRule{
   250  					{
   251  						Index:      "_all",
   252  						Permission: "admin",
   253  					},
   254  					{
   255  						Index:      "_test",
   256  						Permission: "write",
   257  					},
   258  				},
   259  				Username: "test-user",
   260  			}},
   261  			&ElasticSearchACLConfig{
   262  				ACLs: []ElasticSearchACL{
   263  					{
   264  						Username: "test-user",
   265  						Rules: []ElasticsearchACLRule{
   266  							{
   267  								Index:      "_rw",
   268  								Permission: "write",
   269  							},
   270  							{
   271  								Index:      "_all",
   272  								Permission: "admin",
   273  							},
   274  							{
   275  								Index:      "_test",
   276  								Permission: "write",
   277  							},
   278  						}},
   279  					{
   280  						Username: "test-user2",
   281  						Rules: []ElasticsearchACLRule{
   282  							{
   283  								Index:      "_all",
   284  								Permission: "admin",
   285  							},
   286  						}},
   287  				},
   288  			},
   289  		},
   290  	}
   291  	for _, tt := range tests {
   292  		t.Run(tt.name, func(t *testing.T) {
   293  			conf := ElasticSearchACLConfig{
   294  				ACLs:        tt.fields.ACLs,
   295  				Enabled:     tt.fields.Enabled,
   296  				ExtendedAcl: tt.fields.ExtendedAcl,
   297  			}
   298  			if got := conf.Add(tt.args.acl); !reflect.DeepEqual(got, tt.want) {
   299  				t.Errorf("Add() = %v, want %v", got, tt.want)
   300  			}
   301  		})
   302  	}
   303  }
   304  
   305  func TestElasticSearchACLConfig_Delete(t *testing.T) {
   306  	type fields struct {
   307  		ACLs        []ElasticSearchACL
   308  		Enabled     bool
   309  		ExtendedAcl bool
   310  	}
   311  	type args struct {
   312  		acl ElasticSearchACL
   313  	}
   314  	tests := []struct {
   315  		name   string
   316  		fields fields
   317  		args   args
   318  		want   *ElasticSearchACLConfig
   319  	}{
   320  		{
   321  			"multiple",
   322  			fields{
   323  				ACLs: []ElasticSearchACL{
   324  					{
   325  						Username: "test-user",
   326  						Rules: []ElasticsearchACLRule{
   327  							{
   328  								Index:      "_all",
   329  								Permission: "admin",
   330  							},
   331  							{
   332  								Index:      "_rw",
   333  								Permission: "readwrite",
   334  							},
   335  							{
   336  								Index:      "_test",
   337  								Permission: "write",
   338  							},
   339  						}},
   340  					{
   341  						Username: "test-user2",
   342  						Rules: []ElasticsearchACLRule{
   343  							{
   344  								Index:      "_all",
   345  								Permission: "admin",
   346  							},
   347  						}},
   348  				},
   349  				Enabled:     false,
   350  				ExtendedAcl: false,
   351  			},
   352  			args{acl: ElasticSearchACL{
   353  				Username: "test-user",
   354  				Rules: []ElasticsearchACLRule{
   355  					{
   356  						Index:      "_all",
   357  						Permission: "admin",
   358  					},
   359  					{
   360  						Index:      "_rw",
   361  						Permission: "readwrite",
   362  					},
   363  				}}},
   364  			&ElasticSearchACLConfig{
   365  				ACLs: []ElasticSearchACL{
   366  					{
   367  						Username: "test-user",
   368  						Rules: []ElasticsearchACLRule{
   369  							{
   370  								Index:      "_test",
   371  								Permission: "write",
   372  							},
   373  						}},
   374  					{
   375  						Username: "test-user2",
   376  						Rules: []ElasticsearchACLRule{
   377  							{
   378  								Index:      "_all",
   379  								Permission: "admin",
   380  							},
   381  						}},
   382  				},
   383  				Enabled:     false,
   384  				ExtendedAcl: false,
   385  			},
   386  		},
   387  	}
   388  	for _, tt := range tests {
   389  		t.Run(tt.name, func(t *testing.T) {
   390  			conf := ElasticSearchACLConfig{
   391  				ACLs:        tt.fields.ACLs,
   392  				Enabled:     tt.fields.Enabled,
   393  				ExtendedAcl: tt.fields.ExtendedAcl,
   394  			}
   395  			if got := conf.Delete(tt.args.acl); !reflect.DeepEqual(got, tt.want) {
   396  				t.Errorf("Delete() = %v, want %v", got, tt.want)
   397  			}
   398  		})
   399  	}
   400  }