package auth

import (
	"html/template"
	"net/http"
	"net/http/httptest"
	"testing"
)

func TestGetGrantCodeSuccess(t *testing.T) {
	t.Parallel()
	store := NewMemstore()
	testContext := Context{
		template: template.Must(template.New(getGrantTemplateName).Parse("Get auth grant")),
		clients:  store,
		grants:   store,
		profiles: store,
		tokens:   store,
	}
	w := httptest.NewRecorder()
	req, err := http.NewRequest("GET", "https://test.auth.secondbit.org/oauth2/grant", nil)
	if err != nil {
		t.Fatal("Can't build request:", err)
	}
	// see OAuth 2.0 spec, section 4.1.1
	req.URL.Query().Set("response_type", "code")
	req.URL.Query().Set("client_id", "test_client_id")
	req.URL.Query().Set("redirect_uri", "https://test.secondbit.org/redirect")
	req.URL.Query().Set("scope", "testscope")
	req.URL.Query().Set("state", "my super secure state string")
	GetGrantHandler(w, req, testContext)
	if w.Code != http.StatusOK {
		t.Errorf("Expected status code to be %d, got %d", http.StatusOK, w.Code)
	}
	if w.Body.String() != "Get auth grant" {
		t.Errorf("Expected body to be `%s`, got `%s`", "Get auth grant", w.Body.String())
	}
}
