github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/services/v0/download_test.go (about)

     1  package v0
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"testing"
     7  
     8  	v0 "github.com/authzed/authzed-go/proto/authzed/api/v0"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestDeveloperDownload(t *testing.T) {
    13  	require := require.New(t)
    14  
    15  	store := NewInMemoryShareStore("flavored")
    16  	devsrv := NewDeveloperServer(store)
    17  	handler := downloadHandler(store)
    18  
    19  	sresp, err := devsrv.Share(context.Background(), &v0.ShareRequest{
    20  		Schema:            "s",
    21  		RelationshipsYaml: "ry",
    22  		ValidationYaml:    "vy",
    23  		AssertionsYaml:    "ay",
    24  	})
    25  	require.NoError(err)
    26  
    27  	tests := []struct {
    28  		name         string
    29  		method       string
    30  		ref          string
    31  		expectedCode int
    32  		expectedBody string
    33  	}{
    34  		{
    35  			name:         "valid",
    36  			method:       http.MethodGet,
    37  			ref:          sresp.ShareReference,
    38  			expectedCode: http.StatusOK,
    39  			expectedBody: `schema: s
    40  relationships: ry
    41  validation: vy
    42  assertions: ay
    43  `,
    44  		},
    45  		{
    46  			name:         "invalid ref",
    47  			method:       http.MethodGet,
    48  			ref:          "nope",
    49  			expectedCode: http.StatusNotFound,
    50  		},
    51  		{
    52  			name:         "ref with /",
    53  			method:       http.MethodGet,
    54  			ref:          "ref/hasslash",
    55  			expectedCode: http.StatusBadRequest,
    56  			expectedBody: "ref may not contain a '/'",
    57  		},
    58  		{
    59  			name:         "invalid params",
    60  			method:       http.MethodGet,
    61  			ref:          "",
    62  			expectedCode: http.StatusBadRequest,
    63  			expectedBody: "ref is missing",
    64  		},
    65  		{
    66  			name:         "invalid method",
    67  			method:       http.MethodPost,
    68  			ref:          "ref",
    69  			expectedCode: http.StatusMethodNotAllowed,
    70  		},
    71  	}
    72  	for _, test := range tests {
    73  		test := test
    74  		t.Run(test.name, func(t *testing.T) {
    75  			url := downloadPath + test.ref
    76  			require.HTTPStatusCode(handler, test.method, url, nil, test.expectedCode)
    77  			if len(test.expectedBody) > 0 {
    78  				require.HTTPBodyContains(handler, test.method, url, nil, test.expectedBody)
    79  			}
    80  		})
    81  	}
    82  }