<commit_before>package basecrm

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
	"sync"
	"testing"
)

type fakeHTTPClient struct {
	Answer     func(*http.Request) string
	StatusCode int
	requests   []*http.Request
	sync.Mutex
}

func (fc *fakeHTTPClient) RoundTrip(req *http.Request) (*http.Response, error) {
	fc.Lock()
	fc.requests = append(fc.requests, req)
	fc.Unlock()
	res := &http.Response{
		Status:     fmt.Sprintf("%d %s", fc.StatusCode, http.StatusText(fc.StatusCode)),
		StatusCode: fc.StatusCode,
		Header:     make(http.Header),
		Body:       ioutil.NopCloser(strings.NewReader(fc.Answer(req))),
	}
	return res, nil
}

func (fc *fakeHTTPClient) Post(url string, data url.Values) (*http.Response, error) {
	body := strings.NewReader(data.Encode())
	req, err := http.NewRequest("POST", url, body)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	return fc.RoundTrip(req)
}

func TestAuthentication(t *testing.T) {
	fc := &fakeHTTPClient{
		Answer: func(req *http.Request) string {
			return fmt.Sprintf(`{
				"authentication": {
					"token": "TOKEN_%s"
				}
			}`, req.FormValue("email"))
		},
		StatusCode: http.StatusOK,
	}
	c = fc
	email, password := "<EMAIL>", "<PASSWORD>"
	s := NewSession(email, password)
	req := fc.requests[0]
	if method := req.Method; method != "POST" {
		t.Errorf("wrong method: %s", method)
	}
	if url := req.URL; url.String() != "https://sales.futuresimple.com/api/v1/authentication.json" {
		t.Errorf("wrong url: %s", url)
	}
	if token := s.Token; token != "<EMAIL>" {
		t.Errorf("bad token: %s", token)
	}
}<commit_msg>Add test to cover authentication failure.<commit_after>package basecrm

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
	"sync"
	"testing"
)

type fakeHTTPClient struct {
	Answer     func(*http.Request) string
	StatusCode int
	requests   []*http.Request
	sync.Mutex
}

func (fc *fakeHTTPClient) RoundTrip(req *http.Request) (*http.Response, error) {
	fc.Lock()
	fc.requests = append(fc.requests, req)
	fc.Unlock()
	res := &http.Response{
		Status:     fmt.Sprintf("%d %s", fc.StatusCode, http.StatusText(fc.StatusCode)),
		StatusCode: fc.StatusCode,
		Header:     make(http.Header),
		Body:       ioutil.NopCloser(strings.NewReader(fc.Answer(req))),
	}
	return res, nil
}

func (fc *fakeHTTPClient) Post(url string, data url.Values) (*http.Response, error) {
	body := strings.NewReader(data.Encode())
	req, err := http.NewRequest("POST", url, body)
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	return fc.RoundTrip(req)
}

func TestAuthenticationSuccess(t *testing.T) {
	fc := &fakeHTTPClient{
		Answer: func(req *http.Request) string {
			return fmt.Sprintf(`{
				"authentication": {
					"token": "TOKEN_%s"
				}
			}`, req.FormValue("email"))
		},
		StatusCode: http.StatusOK,
	}
	c = fc
	email, password := "<EMAIL>", "<PASSWORD>"
	s := NewSession(email, password)
	req := fc.requests[0]
	if method := req.Method; method != "POST" {
		t.Errorf("wrong method: %s", method)
	}
	if url := req.URL; url.String() != "https://sales.futuresimple.com/api/v1/authentication.json" {
		t.Errorf("wrong url: %s", url)
	}
	if token := s.Token; token != "<EMAIL>" {
		t.Errorf("bad token: %s", token)
	}
}

func TestAuthenticationFailure(t *testing.T) {
	fc := &fakeHTTPClient{
		Answer: func(req *http.Request) string {
			return `{
				"authentication": {
				}
			}`
		},
		StatusCode: http.StatusUnauthorized,
	}
	c = fc
	email, password := "<EMAIL>", "<PASSWORD>"
	s := NewSession(email, password)
	req := fc.requests[0]
	if method := req.Method; method != "POST" {
		t.Errorf("wrong method: %s", method)
	}
	if url := req.URL; url.String() != "https://sales.futuresimple.com/api/v1/authentication.json" {
		t.Errorf("wrong url: %s", url)
	}
	if token := s.Token; token != "" {
		t.Errorf("bad token: %s", token)
	}
}
{'fraction_non_alphanumeric': 0.17938931297709923, 'fraction_numerical': 0.0015267175572519084, 'mean_word_length': 3.9384422110552766, 'pattern_counts': {'":': 5, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, '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': '1496060', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1480, 'n_words': 315}