summary refs log tree commit diff stats
BranchCommit messageAuthorAge
masterMerge branch 'victorbnl-viewnior-config'toonn2 years
 
TagDownloadAuthorAge
v1.9.3ranger-1.9.3.tar.gz  toonn5 years
v1.9.2ranger-1.9.2.tar.gz  toonn6 years
v1.9.1ranger-1.9.1.tar.gz  Wojciech Siewierski7 years
v1.9.0ranger-1.9.0.tar.gz  hut7 years
v1.9.0b6ranger-1.9.0b6.tar.gz  Wojciech Siewierski7 years
v1.9.0b5ranger-1.9.0b5.tar.gz  nfnty8 years
v1.9.0b4ranger-1.9.0b4.tar.gz  nfnty8 years
v1.9.0b3ranger-1.9.0b3.tar.gz  nfnty8 years
v1.9.0b2ranger-1.9.0b2.tar.gz  nfnty8 years
v1.9.0b1ranger-1.9.0b1.tar.gz  nfnty8 years
v1.8.1ranger-1.8.1.tar.gz  nfnty8 years
v1.8.0ranger-1.8.0.tar.gz  hut8 years
v1.7.2ranger-1.7.2.tar.gz  hut9 years
v1.7.1ranger-1.7.1.tar.gz  hut10 years
v1.7.0-emacsranger-1.7.0-emacs.tar.gz  hut10 years
v1.7.0ranger-1.7.0.tar.gz  hut10 years
v1.6.1ranger-1.6.1.tar.gz  hut11 years
v1.6.0ranger-1.6.0.tar.gz  hut12 years
v1.5.5ranger-1.5.5.tar.gz  hut12 years
v1.5.4ranger-1.5.4.tar.gz  hut13 years
v1.5.3ranger-1.5.3.tar.gz  hut13 years
v1.5.2ranger-1.5.2.tar.gz  hut13 years
v1.5.1ranger-1.5.1.tar.gz  hut13 years
v1.5.0ranger-1.5.0.tar.gz  hut13 years
v1.4.4ranger-1.4.4.tar.gz  hut13 years
v1.4.3ranger-1.4.3.tar.gz  hut14 years
v1.4.2ranger-1.4.2.tar.gz  hut14 years
v1.4.1ranger-1.4.1.tar.gz  hut14 years
v1.4.0ranger-1.4.0.tar.gz  hut14 years
v1.2.3ranger-1.2.3.tar.gz  hut14 years
v1.2.2ranger-1.2.2.tar.gz  hut14 years
v1.2.1ranger-1.2.1.tar.gz  hut14 years
v1.2.0ranger-1.2.0.tar.gz  hut14 years
v1.1.2ranger-1.1.2.tar.gz  hut14 years
v1.1.1ranger-1.1.1.tar.gz  hut14 years
v1.1.0ranger-1.1.0.tar.gz  hut14 years
v1.0.4ranger-1.0.4.tar.gz  hut15 years
v1.0.3ranger-1.0.3.tar.gz  hut15 years
v1.0.2ranger-1.0.2.tar.gz  hut15 years
v0.2.6ranger-0.2.6.tar.gz  hut15 years
v1.0.1ranger-1.0.1.tar.gz  hut15 years
v0.2.5ranger-0.2.5.tar.gz  hut15 years
v0.2.4ranger-0.2.4.tar.gz  hut15 years
v0.2.3ranger-0.2.3.tar.gz  hut15
package svc // import "github.com/getwtxt/getwtxt/svc"

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"

	"github.com/getwtxt/registry"
)

var apiPostUserCases = []struct {
	name    string
	nick    string
	uri     string
	wantErr bool
}{
	{
		name:    "Known Good User",
		nick:    "gbmor",
		uri:     "https://gbmor.dev/twtxt.txt",
		wantErr: false,
	},
	{
		name:    "Missing URI",
		nick:    "missinguri",
		uri:     "",
		wantErr: true,
	},
	{
		name:    "Missing Nickname",
		nick:    "",
		uri:     "https://example.com/twtxt.txt",
		wantErr: true,
	},
	{
		name:    "Missing URI and Nickname",
		nick:    "",
		uri:     "",
		wantErr: true,
	},
}

func Test_apiPostUser(t *testing.T) {
	initTestConf()
	portnum := fmt.Sprintf(":%v", confObj.Port)
	twtxtCache = registry.NewIndex()

	for _, tt := range apiPostUserCases {
		t.Run(tt.name, func(t *testing.T) {
			params := url.Values{}
			params.Set("url", tt.uri)
			params.Set("nickname", tt.nick)

			req, err := http.NewRequest("POST", "https://localhost"+portnum+"/api/plain/users", strings.NewReader(params.Encode()))
			if err != nil {
				t.Errorf("%v\n", err)
			}

			req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
			rr := httptest.NewRecorder()
			apiEndpointPOSTHandler(rr, req)

			if !tt.wantErr {
				if rr.Code != http.StatusOK {
					t.Errorf("Received unexpected non-200 response: %v\n", rr.Code)
				}
			} else {
				if rr.Code != http.StatusBadRequest {
					t.Errorf("Expected 400 Bad Request, but received: %v\n", rr.Code)
				}
			}
		})
	}
}