package Auth_test

import (
	"net/http"
	"testing"

	"git.tovijaeschke.xyz/tovi/Capsule/Backend/Database"
	"git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
	"git.tovijaeschke.xyz/tovi/Capsule/Backend/Tests"
)

func Test_Logout(t *testing.T) {
	client, ts, err := Tests.InitTestEnv()
	defer ts.Close()
	if err != nil {
		t.Errorf("Expected nil, recieved %s", err.Error())
		return
	}

	resp, err := client.Get(ts.URL + "/api/v1/logout")
	if err != nil {
		t.Errorf("Expected user record, recieved %s", err.Error())
		return
	}

	if resp.StatusCode != http.StatusOK {
		t.Errorf("Expected %d, recieved %d", http.StatusOK, resp.StatusCode)
		return
	}

	var session Models.Session

	u, err := Database.GetUserByUsername("test")
	if err != nil {
		t.Errorf("Expected user record, recieved %s", err.Error())
		return
	}

	err = Database.DB.First(&session, "user_id = ?", u.ID.String()).Error
	if err == nil {
		t.Errorf("Expected no session record, recieved %s", session.UserID)
		return
	}
}
