<commit_before>// Integration test
// This needs some env variables to run the tests
//      APP_TOKEN
//      APP_SECRET
//      ACCESS_TOKEN
//      ACCESS_SECRET
package copy

import (
	"bytes"
	"fmt"
	"math/rand"
	"net/url"
	"os"
	"strings"
	"testing"
	"time"
)

const (
	appTokenEnv     = "APP_TOKEN"
	appSecretEnv    = "APP_SECRET"
	accessTokenEnv  = "ACCESS_TOKEN"
	accessSecretEnv = "ACCESS_SECRET"
)

var (
	appToken     string
	appSecret    string
	accessToken  string
	accessSecret string
	session      *Session
)

func setup() {
	appToken = os.Getenv(appTokenEnv)
	appSecret = os.Getenv(appSecretEnv)
	accessToken = os.Getenv(accessTokenEnv)
	accessSecret = os.Getenv(accessSecretEnv)

	session, _ = NewSession(
		AppToken{
			Token: appToken,
			Key:   appSecret,
		},
		AccessToken{
			Token: accessToken,
			Key:   accessSecret,
		},
	)
}

func tearDown() {

}

// Checks if the credentials for the integration tests are set in the env vars
func TestCredentialData(t *testing.T) {
	setup()
	defer tearDown()

	if appToken == "" {
		t.Error("Expected", appTokenEnv, "env var")
	}

	if appSecret == "" {
		t.Error("Expected", appSecretEnv, "env var")
	}

	if accessToken == "" {
		t.Error("Expected", accessTokenEnv, "env var")
	}

	if accessSecret == "" {
		t.Error("Expected", accessSecretEnv, "env var")
	}
}

// Check the GET request in a valid copy resource
func TestGetRequest(t *testing.T) {
	setup()
	defer tearDown()

	resp, err := session.Get(strings.Join([]string{defaultResourcesUrl, "user"}, "/"), nil, defaultHttpClient)

	if err != nil {
		t.Error("Expected no error in GET request")
	}

	if resp.StatusCode != 200 {
		t.Errorf("Response status error shouldn't be: %v", resp.StatusCode)
	}
}

// Check the GET request in an invalid copy resource with valid credentials
func TestGetRequestWrongResource(t *testing.T) {
	setup()
	defer tearDown()

	resp, _ := session.Get(strings.Join([]string{defaultResourcesUrl, "you shall not pass"}, "/"), nil, defaultHttpClient)

	if resp.StatusCode != 400 {
		t.Errorf("Response status error should be: %v", resp.StatusCode)
	}
}

// Check the GET request in a valid copy resource with wrong credentials
func TestGetRequestWrongCredentials(t *testing.T) {
	setup()
	defer tearDown()

	session.TokenCreds.Secret = "<PASSWORD>!"

	resp, _ := session.Get(strings.Join([]string{defaultResourcesUrl, "user"}, "/"), nil, defaultHttpClient)

	if resp.StatusCode != 400 {
		t.Errorf("Response status error should be: %v", resp.StatusCode)
	}
}

// Check the PUT request in a valid copy resource
func TestPutRequest(t *testing.T) {
	setup()
	defer tearDown()

	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	values := url.Values{
		"first_name": {fmt.Sprintf("TestName %d", r.Intn(100))},
		"last_name":  {fmt.Sprintf("TestSurname %d", r.Intn(100))},
	}

	resp, err := session.Put(strings.Join([]string{defaultResourcesUrl, "user"}, "/"), values, defaultHttpClient)

	defer resp.Body.Close()

	if err != nil {
		t.Error("Expected no error in POST request")
	}

	if resp.StatusCode != 200 {
		t.Errorf("Response status error shouldn't be: %v", resp.StatusCode)
	}

	// Get the response body to check the content
	buf := new(bytes.Buffer)
	_, err = buf.ReadFrom(resp.Body)

	if err != nil {
		t.Errorf("Error reading the response body")
	}<commit_msg>Minor changes<commit_after>// Integration test
// This needs some env variables to run the tests
//      APP_TOKEN
//      APP_SECRET
//      ACCESS_TOKEN
//      ACCESS_SECRET
package copy

import (
	"bytes"
	"fmt"
	"math/rand"
	"net/url"
	"os"
	"strings"
	"testing"
	"time"
)

const (
	appTokenEnv     = "APP_TOKEN"
	appSecretEnv    = "APP_SECRET"
	accessTokenEnv  = "ACCESS_TOKEN"
	accessSecretEnv = "ACCESS_SECRET"
)

var (
	appToken     string
	appSecret    string
	accessToken  string
	accessSecret string
	session      *Session
)

func setupIntegration() {
	appToken = os.Getenv(appTokenEnv)
	appSecret = os.Getenv(appSecretEnv)
	accessToken = os.Getenv(accessTokenEnv)
	accessSecret = os.Getenv(accessSecretEnv)

	session, _ = NewSession(
		AppToken{
			Token: appToken,
			Key:   appSecret,
		},
		AccessToken{
			Token: accessToken,
			Key:   accessSecret,
		},
	)
}

func tearDownIntegration() {

}

// Checks if the credentials for the integration tests are set in the env vars
func TestCredentialData(t *testing.T) {
	setupIntegration()
	defer tearDownIntegration()

	if appToken == "" {
		t.Error("Expected", appTokenEnv, "env var")
	}

	if appSecret == "" {
		t.Error("Expected", appSecretEnv, "env var")
	}

	if accessToken == "" {
		t.Error("Expected", accessTokenEnv, "env var")
	}

	if accessSecret == "" {
		t.Error("Expected", accessSecretEnv, "env var")
	}
}

// Check the GET request in a valid copy resource
func TestGetRequest(t *testing.T) {
	setupIntegration()
	defer tearDownIntegration()

	resp, err := session.Get(strings.Join([]string{defaultResourcesUrl, "user"}, "/"), nil, defaultHttpClient)

	if err != nil {
		t.Error("Expected no error in GET request")
	}

	if resp.StatusCode != 200 {
		t.Errorf("Response status error shouldn't be: %v", resp.StatusCode)
	}
}

// Check the GET request in an invalid copy resource with valid credentials
func TestGetRequestWrongResource(t *testing.T) {
	setupIntegration()
	defer tearDownIntegration()

	resp, _ := session.Get(strings.Join([]string{defaultResourcesUrl, "you shall not pass"}, "/"), nil, defaultHttpClient)

	if resp.StatusCode != 400 {
		t.Errorf("Response status error should be: %v", resp.StatusCode)
	}
}

// Check the GET request in a valid copy resource with wrong credentials
func TestGetRequestWrongCredentials(t *testing.T) {
	setupIntegration()
	defer tearDownIntegration()

	session.TokenCreds.Secret = "<PASSWORD>!"

	resp, _ := session.Get(strings.Join([]string{defaultResourcesUrl, "user"}, "/"), nil, defaultHttpClient)

	if resp.StatusCode != 400 {
		t.Errorf("Response status error should be: %v", resp.StatusCode)
	}
}

// Check the PUT request in a valid copy resource
func TestPutRequest(t *testing.T) {
	setupIntegration()
	defer tearDownIntegration()

	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	values := url.Values{
		"first_name": {fmt.Sprintf("TestName %d", r.Intn(100))},
		"last_name":  {fmt.Sprintf("TestSurname %d", r.Intn(100))},
	}

	resp, err := session.Put(strings.Join([]string{defaultResourcesUrl, "user"}, "/"), values, defaultHttpClient)

	defer resp.Body.Close()

	if err != nil {
		t.Error("Expected no error in POST request")
	}

	if resp.StatusCode != 200 {
		t.Errorf("Response status error shouldn't be: %v", resp.StatusCode)
	}

	// Get the response body to check the content
	buf := new(bytes.Buffer)
	_, err = buf.ReadFrom(resp.Body)

	if err != nil {
		t.Errorf("Error reading the response body")
	}
{'fraction_non_alphanumeric': 0.13672684226102397, 'fraction_numerical': 0.005327019828351583, 'mean_word_length': 4.051569506726457, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1315421', 'n_tokens_mistral': 2373, 'n_tokens_neox': 2257, 'n_words': 601}