<commit_before>package integration

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/http/httputil"
	"os/exec"
	"strings"
	"testing"

	"github.com/sclevine/spec"
	"github.com/stretchr/testify/require"
)

var _ = suite("database/user/get", func(t *testing.T, when spec.G, it spec.S) {
	var (
		expect *require.Assertions
		server *httptest.Server
	)

	it.Before(func() {
		expect = require.New(t)

		server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			switch req.URL.Path {
			case "/v2/databases/some-database-id/users/some-user-id":
				auth := req.Header.Get("Authorization")
				if auth != "Bearer some-magic-token" {
					w.WriteHeader(http.StatusTeapot)
				}

				if req.Method != "GET" {
					w.WriteHeader(http.StatusTeapot)
					return
				}

				w.Write([]byte(databaseUserGetResponse))
			default:
				dump, err := httputil.DumpRequest(req, true)
				if err != nil {
					t.Fatal("failed to dump request")
				}

				t.Fatalf("received unknown request: %s", dump)
			}
		}))
	})

	when("all required flags are passed", func() {
		it("gets the database user", func() {
			cmd := exec.Command(builtBinaryPath,
				"-t", "some-magic-token",
				"-u", server.URL,
				"database",
				"user",
				"get",
				"some-database-id",
				"some-user-id",
			)

			output, err := cmd.CombinedOutput()
			expect.NoError(err, fmt.Sprintf("received error output: %s", output))
			expect.Equal(strings.TrimSpace(databaseUserGetOutput), strings.TrimSpace(string(output)))
		})
	})

	when("no header is passed", func() {
		it("does not display the header", func() {
			cmd := exec.Command(builtBinaryPath,
				"-t", "some-magic-token",
				"-u", server.URL,
				"database",
				"user",
				"get",
				"some-database-id",
				"some-user-id",
				"--no-header",
			)

			output, err := cmd.CombinedOutput()
			expect.NoError(err, fmt.Sprintf("received error output: %s", output))
			expect.Equal(strings.TrimSpace(databaseUserGetNoHeaderOutput), strings.TrimSpace(string(output)))
		})
	})

	when("format is passed", func() {
		it("presents the columns requested", func() {
			cmd := exec.Command(builtBinaryPath,
				"-t", "some-magic-token",
				"-u", server.URL,
				"database",
				"user",
				"get",
				"some-database-id",
				"some-user-id",
				"--format", "Name",
			)

			output, err := cmd.CombinedOutput()
			expect.NoError(err, fmt.Sprintf("received error output: %s", output))
			expect.Equal(strings.TrimSpace(databaseUserGetFormatOutput), strings.TrimSpace(string(output)))
		})
	})
})

const databaseUserGetOutput = `
Name            Role      Password
some-user-id    normal    jge5lfxtzhx42iff
`
const databaseUserGetNoHeaderOutput = `
some-user-id    normal    jge5lfxtzhx42iff
`
const databaseUserGetFormatOutput = `
Name
some-user-id
`
const databaseUserGetResponse = `
{
  "user": {
    "name": "some-user-id",
    "role": "normal",
    "password": "<PASSWORD>"
  }
}
`<commit_msg>adds global output flag test<commit_after>package integration

import (
	"fmt"
	"net/http"
	"net/http/httptest"
	"net/http/httputil"
	"os/exec"
	"strings"
	"testing"

	"github.com/sclevine/spec"
	"github.com/stretchr/testify/require"
)

var _ = suite("database/user/get", func(t *testing.T, when spec.G, it spec.S) {
	var (
		expect *require.Assertions
		server *httptest.Server
	)

	it.Before(func() {
		expect = require.New(t)

		server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
			switch req.URL.Path {
			case "/v2/databases/some-database-id/users/some-user-id":
				auth := req.Header.Get("Authorization")
				if auth != "Bearer some-magic-token" {
					w.WriteHeader(http.StatusTeapot)
				}

				if req.Method != "GET" {
					w.WriteHeader(http.StatusTeapot)
					return
				}

				w.Write([]byte(databaseUserGetResponse))
			default:
				dump, err := httputil.DumpRequest(req, true)
				if err != nil {
					t.Fatal("failed to dump request")
				}

				t.Fatalf("received unknown request: %s", dump)
			}
		}))
	})

	when("all required flags are passed", func() {
		it("gets the database user", func() {
			cmd := exec.Command(builtBinaryPath,
				"-t", "some-magic-token",
				"-u", server.URL,
				"database",
				"user",
				"get",
				"some-database-id",
				"some-user-id",
			)

			output, err := cmd.CombinedOutput()
			expect.NoError(err, fmt.Sprintf("received error output: %s", output))
			expect.Equal(strings.TrimSpace(databaseUserGetOutput), strings.TrimSpace(string(output)))
		})
	})

	when("no header is passed", func() {
		it("does not display the header", func() {
			cmd := exec.Command(builtBinaryPath,
				"-t", "some-magic-token",
				"-u", server.URL,
				"database",
				"user",
				"get",
				"some-database-id",
				"some-user-id",
				"--no-header",
			)

			output, err := cmd.CombinedOutput()
			expect.NoError(err, fmt.Sprintf("received error output: %s", output))
			expect.Equal(strings.TrimSpace(databaseUserGetNoHeaderOutput), strings.TrimSpace(string(output)))
		})
	})

	when("format is passed", func() {
		it("presents the columns requested", func() {
			cmd := exec.Command(builtBinaryPath,
				"-t", "some-magic-token",
				"-u", server.URL,
				"database",
				"user",
				"get",
				"some-database-id",
				"some-user-id",
				"--format", "Name",
			)

			output, err := cmd.CombinedOutput()
			expect.NoError(err, fmt.Sprintf("received error output: %s", output))
			expect.Equal(strings.TrimSpace(databaseUserGetFormatOutput), strings.TrimSpace(string(output)))
		})
	})

	when("the output is json", func() {
		it("prints json", func() {
			cmd := exec.Command(builtBinaryPath,
				"-t", "some-magic-token",
				"-u", server.URL,
				"-o", "json",
				"database",
				"user",
				"get",
				"some-database-id",
				"some-user-id",
			)

			output, err := cmd.CombinedOutput()
			expect.NoError(err, fmt.Sprintf("received error output: %s", output))
			expect.JSONEq(databaseUserGetJSONOutput, string(output))
		})
	})
})

const databaseUserGetOutput = `
Name            Role      Password
some-user-id    normal    <PASSWORD>
`
const databaseUserGetNoHeaderOutput = `
some-user-id    normal    jge5lfxtzhx42iff
`
const databaseUserGetFormatOutput = `
Name
some-user-id
`
const databaseUserGetJSONOutput = `
[{ "name": "some-user-id", "role": "normal", "password": "<PASSWORD>" }]
`
const databaseUserGetResponse = `
{
  "user": {
    "name": "some-user-id",
    "role": "normal",
    "password": "<PASSWORD>"
  }
}
`
{'fraction_non_alphanumeric': 0.169893139228744, 'fraction_numerical': 0.0017035775127768314, 'mean_word_length': 3.3605671843349088, 'pattern_counts': {'":': 13, '<': 7, '<?xml version=': 0, '>': 7, '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': '5270238', 'n_tokens_mistral': 2689, 'n_tokens_neox': 2242, 'n_words': 430}