<commit_before>package flickr

import (
	"fmt"
	"net/url"
	"testing"
)

// testing keys were published at http://www.wackylabs.net/2011/12/oauth-and-flickr-part-2/
func getTestClient() *FlickrClient {
	args := url.Values{}
	args.Set("oauth_nonce", "C2F26CD5C075BA9050AD8EE90644CF29")
	args.Set("oauth_timestamp", "1316657628")
	args.Set("oauth_consumer_key", "<KEY>")
	args.Set("oauth_signature_method", "HMAC-SHA1")
	args.Set("oauth_version", "1.0")
	args.Set("oauth_callback", "http://www.wackylabs.net/oauth/test")

	return &FlickrClient{
		EndpointUrl: "http://www.flickr.com/services/oauth/request_token",
		Method:      "GET",
		Args:        args,
		ApiSecret:   "1a3c208e172d3edc",
	}
}

func TestGetSigningBaseString(t *testing.T) {
	c := getTestClient()

	ret := getSigningBaseString(c)
	expected := "GET&http%3A%2F%2Fwww.flickr.com%2Fservices%2Foauth%2Frequest_token&" +
		"oauth_callback%3Dhttp%253A%252F%252Fwww.wackylabs.net%252F" +
		"oauth%252Ftest%26oauth_consumer_key%3D768fe946d252b119746fda82e1599980%26" +
		"oauth_nonce%3DC2F26CD5C075BA9050AD8EE90644CF29%26" +
		"oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1316657628%26" +
		"oauth_version%3D1.0"

	if ret != expected {
		t.Error("Expected", expected, "found", ret)
	}
}

func TestSign(t *testing.T) {
	c := getTestClient()

	c.Sign("token1<PASSWORD>secret")
	expected := "dXyfrCetFSTpzD3djSrkFhj0MIQ="
	signed := c.Args.Get("oauth_signature")

	if signed != expected {
		t.Error("Expected", expected, "found", signed)
	}
	fmt.Println(signed)

	// test empty token_secret
	c.Sign("")
	expected = "0fhNGlzpFNAsTme/hDfUb5HPB5U="
	signed = c.Args.Get("oauth_signature")

	if signed != expected {
		t.Error("Expected", expected, "found", signed)
	}
}

func TestGenerateNonce(t *testing.T) {
	var nonce string
	nonce = generateNonce()
	if len(nonce) != 8 {
		t.Error("Expected string with length of 8, found", nonce)
	}
}

func TestGetDefaultArgs(t *testing.T) {
	args := getDefaultArgs()
	check := func(key string) {
		val := args.Get(key)
		if val == "" {
			t.Error("Found empty string for", key)
		}
	}

	check("oauth_version")
	check("oauth_signature_method")
	check("oauth_nonce")
	check("oauth_timestamp")
}

func TestParseRequestToken(t *testing.T) {
	tok := RequestToken{}
	in := "oauth_callback_confirmed=true&oauth_token=<PASSWORD>&oauth_token_secret=8700d234e3fc00c6"
	expected := RequestToken{true, "<PASSWORD>", "8700d2<PASSWORD>"}

	err := tok.Parse(in)
	if err != nil {
		t.Error("Error:", err)
	}

	if tok != expected {
		t.Error("Expected", expected, "found", tok)
	}

	err = tok.Parse("notA%%%ValidUrl")
	if err == nil {
		t.Error("Parsing an invalid URL string should rise an error")
	}
}

func TestGetRequestToken(t *testing.T) {
	//GetRequestToken("<PASSWORD>", "<PASSWORD>")
}<commit_msg>added mocking system for flickr calls<commit_after>package flickr

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/url"
	"path"
	"reflect"
	"testing"
)

func expect(t *testing.T, a interface{}, b interface{}) {
	if a != b {
		t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
	}
}

// testing keys were published at http://www.wackylabs.net/2011/12/oauth-and-flickr-part-2/
func getTestClient() *FlickrClient {
	args := url.Values{}
	args.Set("oauth_nonce", "C2F26CD5C075BA9050AD8EE90644CF29")
	args.Set("oauth_timestamp", "1316657628")
	args.Set("oauth_consumer_key", "768fe946d252b119746fda82e1599980")
	args.Set("oauth_signature_method", "HMAC-SHA1")
	args.Set("oauth_version", "1.0")
	args.Set("oauth_callback", "http://www.wackylabs.net/oauth/test")

	return &FlickrClient{
		EndpointUrl: "http://www.flickr.com/services/oauth/request_token",
		Method:      "GET",
		Args:        args,
		ApiSecret:   "1a3c208e172d3edc",
	}
}

// mock the Flickr API
type RewriteTransport struct {
	Transport http.RoundTripper
	URL       *url.URL
}

func (t RewriteTransport) RoundTrip(req *http.Request) (*http.Response, error) {
	req.URL.Scheme = t.URL.Scheme
	req.URL.Host = t.URL.Host
	req.URL.Path = path.Join(t.URL.Path, req.URL.Path)
	rt := t.Transport
	if rt == nil {
		rt = http.DefaultTransport
	}
	return rt.RoundTrip(req)
}

func flickrMock(code int, body string, contentType string) (*httptest.Server, *http.Client) {
	if contentType == "" {
		contentType = "text/plain;charset=UTF-8"
	}

	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.WriteHeader(code)
		w.Header().Set("Content-Type", contentType)
		fmt.Fprintln(w, body)
	}))

	u, _ := url.Parse(server.URL)

	return server, &http.Client{Transport: RewriteTransport{URL: u}}
}

func TestGetSigningBaseString(t *testing.T) {
	c := getTestClient()

	ret := getSigningBaseString(c)
	expected := "GET&http%3A%2F%2Fwww.flickr.com%2Fservices%2Foauth%2Frequest_token&" +
		"oauth_callback%3Dhttp%253A%252F%252Fwww.wackylabs.net%252F" +
		"oauth%252Ftest%26oauth_consumer_key%3D768fe946d252b119746fda82e1599980%26" +
		"oauth_nonce%3DC2F26CD5C075BA9050AD8EE90644CF29%26" +
		"oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1316657628%26" +
		"oauth_version%3D1.0"

	expect(t, ret, expected)
}

func TestSign(t *testing.T) {
	c := getTestClient()

	c.Sign("token12345secret")
	expected := "dXyfrCetFSTpzD3djSrkFhj0MIQ="
	signed := c.Args.Get("oauth_signature")
	expect(t, signed, expected)

	// test empty token_secret
	c.Sign("")
	expected = "0fhNGlzpFNAsTme/hDfUb5HPB5U="
	signed = c.Args.Get("oauth_signature")
	expect(t, signed, expected)
}

func TestGenerateNonce(t *testing.T) {
	var nonce string
	nonce = generateNonce()
	expect(t, 8, len(nonce))
}

func TestGetDefaultArgs(t *testing.T) {
	args := getDefaultArgs()
	check := func(key string) {
		val := args.Get(key)
		if val == "" {
			t.Error("Found empty string for", key)
		}
	}

	check("oauth_version")
	check("oauth_signature_method")
	check("oauth_nonce")
	check("oauth_timestamp")
}

func TestParseRequestToken(t *testing.T) {
	tok := RequestToken{}
	in := "oauth_callback_confirmed=true&oauth_token=7<PASSWORD>&oauth_token_secret=8700d234e3fc00c6"
	expected := RequestToken{true, "7<PASSWORD>", "<PASSWORD>"}

	err := tok.Parse(in)
	expect(t, nil, err)
	expect(t, tok, expected)

	err = tok.Parse("notA%%%ValidUrl")
	if err == nil {
		t.Error("Parsing an invalid URL string should rise an error")
	}
}

func TestGetRequestToken(t *testing.T) {
	fclient := getTestClient()
	mocked_body := "oauth_callback_confirmed=true&oauth_token=72<PASSWORD>cda5<PASSWORD>&oauth_token_secret=8700d234e3fc00c6"
	server, client := flickrMock(200, mocked_body, "")
	defer server.Close()
	// use the mocked client
	fclient.HTTPClient = client

	tok, err := GetRequestToken(fclient)
	if err != nil {
		t.Error("Unexpected error:", err)
	}

	expect(t, tok.OauthCallbackConfirmed, true)
	expect(t, tok.OauthToken, "7<PASSWORD>")
	expect(t, tok.OauthTokenSecret, "8700d234e3fc00c6")
}
{'fraction_non_alphanumeric': 0.15663176265270506, 'fraction_numerical': 0.055846422338568937, 'mean_word_length': 5.263205828779599, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, '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': '4389352', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2777, 'n_words': 454}