github.com/influxdata/influxdb/v2@v2.7.6/http/document_test.go (about)

     1  package http
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/influxdata/httprouter"
    11  	"github.com/influxdata/influxdb/v2"
    12  	pcontext "github.com/influxdata/influxdb/v2/context"
    13  	"github.com/influxdata/influxdb/v2/kit/platform"
    14  	kithttp "github.com/influxdata/influxdb/v2/kit/transport/http"
    15  	"github.com/influxdata/influxdb/v2/mock"
    16  	influxtesting "github.com/influxdata/influxdb/v2/testing"
    17  	"go.uber.org/zap/zaptest"
    18  )
    19  
    20  var (
    21  	doc1ID = influxtesting.MustIDBase16("020f755c3c082010")
    22  	doc2ID = influxtesting.MustIDBase16("020f755c3c082011")
    23  	doc1   = influxdb.Document{
    24  		ID: doc1ID,
    25  		Meta: influxdb.DocumentMeta{
    26  			Name:        "doc1",
    27  			Type:        "typ1",
    28  			Description: "desc1",
    29  		},
    30  		Content: "content1",
    31  	}
    32  	doc2 = influxdb.Document{
    33  		ID: doc2ID,
    34  		Meta: influxdb.DocumentMeta{
    35  			Name: "doc2",
    36  		},
    37  		Content: "content2",
    38  	}
    39  
    40  	docs = []*influxdb.Document{
    41  		&doc1,
    42  		&doc2,
    43  	}
    44  	docsResp = `{
    45  		"documents":[
    46  			{
    47  				"id": "020f755c3c082010",
    48  				"links": {
    49  					"self": "/api/v2/documents/template/020f755c3c082010"
    50  				},
    51  				"content": "content1",
    52  				"meta": {
    53  					"name": "doc1",
    54  					"type": "typ1",
    55  					"createdAt": "0001-01-01T00:00:00Z",
    56  					"updatedAt": "0001-01-01T00:00:00Z",
    57  					"description": "desc1"
    58  				}
    59  			},
    60  			{
    61  				"id": "020f755c3c082011",
    62  				"links": {
    63  					"self": "/api/v2/documents/template/020f755c3c082011"
    64  				},
    65  				"content": "content2",
    66  				"meta": {
    67  					"name": "doc2",
    68  					"createdAt": "0001-01-01T00:00:00Z",
    69  					"updatedAt": "0001-01-01T00:00:00Z"	
    70  				}
    71  			}
    72  		]
    73  	}`
    74  	findDocsServiceMock = &mock.DocumentService{
    75  		FindDocumentStoreFn: func(context.Context, string) (influxdb.DocumentStore, error) {
    76  			return &mock.DocumentStore{
    77  				FindDocumentsFn: func(ctx context.Context, _ platform.ID) ([]*influxdb.Document, error) {
    78  					return docs, nil
    79  				},
    80  			}, nil
    81  		},
    82  	}
    83  )
    84  
    85  // NewMockDocumentBackend returns a DocumentBackend with mock services.
    86  func NewMockDocumentBackend(t *testing.T) *DocumentBackend {
    87  	return &DocumentBackend{
    88  		log: zaptest.NewLogger(t),
    89  
    90  		DocumentService: mock.NewDocumentService(),
    91  	}
    92  }
    93  
    94  func TestService_handleGetDocuments(t *testing.T) {
    95  	type fields struct {
    96  		DocumentService influxdb.DocumentService
    97  	}
    98  	type args struct {
    99  		authorizer influxdb.Authorizer
   100  		orgID      platform.ID
   101  	}
   102  	type wants struct {
   103  		statusCode  int
   104  		contentType string
   105  		body        string
   106  	}
   107  
   108  	tests := []struct {
   109  		name   string
   110  		fields fields
   111  		args   args
   112  		wants  wants
   113  	}{
   114  		{
   115  			name: "get all documents",
   116  			fields: fields{
   117  				DocumentService: findDocsServiceMock,
   118  			},
   119  			args: args{
   120  				authorizer: mock.NewMockAuthorizer(true, nil),
   121  				orgID:      platform.ID(2),
   122  			},
   123  			wants: wants{
   124  				statusCode:  http.StatusOK,
   125  				contentType: "application/json; charset=utf-8",
   126  				body:        docsResp,
   127  			},
   128  		},
   129  	}
   130  	for _, tt := range tests {
   131  		t.Run(tt.name, func(t *testing.T) {
   132  			documentBackend := NewMockDocumentBackend(t)
   133  			documentBackend.HTTPErrorHandler = kithttp.NewErrorHandler(zaptest.NewLogger(t))
   134  			documentBackend.DocumentService = tt.fields.DocumentService
   135  			h := NewDocumentHandler(documentBackend)
   136  
   137  			r := httptest.NewRequest("GET", "http://any.url", nil)
   138  			qp := r.URL.Query()
   139  			qp.Add("orgID", tt.args.orgID.String())
   140  			r.URL.RawQuery = qp.Encode()
   141  
   142  			r = r.WithContext(pcontext.SetAuthorizer(r.Context(), tt.args.authorizer))
   143  			r = r.WithContext(context.WithValue(r.Context(),
   144  				httprouter.ParamsKey,
   145  				httprouter.Params{
   146  					{
   147  						Key:   "ns",
   148  						Value: "template",
   149  					}}))
   150  			w := httptest.NewRecorder()
   151  			h.handleGetDocuments(w, r)
   152  			res := w.Result()
   153  			content := res.Header.Get("Content-Type")
   154  			body, _ := io.ReadAll(res.Body)
   155  
   156  			if res.StatusCode != tt.wants.statusCode {
   157  				t.Errorf("%q. handleGetDocuments() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
   158  			}
   159  			if tt.wants.contentType != "" && content != tt.wants.contentType {
   160  				t.Errorf("%q. handleGetDocuments() = %v, want %v", tt.name, content, tt.wants.contentType)
   161  			}
   162  			if tt.wants.body != "" {
   163  				if eq, diff, err := jsonEqual(string(body), tt.wants.body); err != nil {
   164  					t.Errorf("%q, handleGetDocuments(). error unmarshaling json %v", tt.name, err)
   165  				} else if !eq {
   166  					t.Errorf("%q. handleGetDocuments() = ***%s***", tt.name, diff)
   167  				}
   168  			}
   169  		})
   170  	}
   171  }