...

Source file src/github.com/go-kivik/kivik/v4/x/server/cluster_test.go

Documentation: github.com/go-kivik/kivik/v4/x/server

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  //go:build !js
    14  
    15  package server
    16  
    17  import (
    18  	"net/http"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/go-kivik/kivik/v4"
    23  	"github.com/go-kivik/kivik/v4/mockdb"
    24  )
    25  
    26  func Test_clusterStatus(t *testing.T) {
    27  	tests := serverTests{
    28  		{
    29  			name:       "cluster status, unauthorized",
    30  			method:     http.MethodGet,
    31  			path:       "/_cluster_setup",
    32  			wantStatus: http.StatusUnauthorized,
    33  			wantJSON: map[string]interface{}{
    34  				"error":  "unauthorized",
    35  				"reason": "User not authenticated",
    36  			},
    37  		},
    38  		{
    39  			name: "cluster status, success",
    40  			client: func() *kivik.Client {
    41  				client, mock, err := mockdb.New()
    42  				if err != nil {
    43  					t.Fatal(err)
    44  				}
    45  				mock.ExpectClusterStatus().
    46  					WillReturn("chicken")
    47  				return client
    48  			}(),
    49  			method:     http.MethodGet,
    50  			path:       "/_cluster_setup",
    51  			authUser:   userAdmin,
    52  			wantStatus: http.StatusOK,
    53  			wantJSON: map[string]string{
    54  				"state": "chicken",
    55  			},
    56  		},
    57  	}
    58  
    59  	tests.Run(t)
    60  }
    61  
    62  func TestClusterSetup(t *testing.T) {
    63  	tests := serverTests{
    64  		{
    65  			name:       "cluster status, unauthorized",
    66  			method:     http.MethodPost,
    67  			path:       "/_cluster_setup",
    68  			wantStatus: http.StatusUnauthorized,
    69  			wantJSON: map[string]string{
    70  				"error":  "unauthorized",
    71  				"reason": "User not authenticated",
    72  			},
    73  		},
    74  		{
    75  			name: "cluster status, success",
    76  			client: func() *kivik.Client {
    77  				client, mock, err := mockdb.New()
    78  				if err != nil {
    79  					t.Fatal(err)
    80  				}
    81  				mock.ExpectClusterSetup().
    82  					WithAction("chicken").
    83  					WillReturnError(nil)
    84  				return client
    85  			}(),
    86  			method:     http.MethodPost,
    87  			authUser:   userAdmin,
    88  			path:       "/_cluster_setup",
    89  			headers:    map[string]string{"Content-Type": "application/json"},
    90  			body:       strings.NewReader(`{"action":"chicken"}`),
    91  			wantStatus: http.StatusOK,
    92  			wantJSON: map[string]bool{
    93  				"ok": true,
    94  			},
    95  		},
    96  	}
    97  
    98  	tests.Run(t)
    99  }
   100  

View as plain text