<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package main

import (
	"encoding/json"
	"strings"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
)

func TestServices(t *testing.T) {
	testsSetup()
	setup()

	Convey("Scenario: reeting a service", t, func() {
		foundSubscriber("service.set", `"success"`, 1)

		Convey("Given my existing service is in progress", func() {
			foundSubscriber("service.find", `[{"id":"1","name":"test","status":"in_progress"},{"id":"2","name":"test","status":"done"}]`, 1)

			Convey("When I do a call to /services/reset", func() {
				params := make(map[string]string)
				params["service"] = "foo"
				resp, err := doRequest("POST", "/services/foo/reset/", params, nil, resetServiceHandler, nil)
				Convey("Then it should return a success message", func() {
					So(err, ShouldBeNil)
					So(string(resp), ShouldEqual, `success`)
				})
			})
		})

		Convey("Given my existing service is errored", func() {
			foundSubscriber("service.find", `[{"id":"1","name":"test","status":"errored"},{"id":"2","name":"test","status":"done"}]`, 1)

			Convey("When I do a call to /services/reset", func() {
				params := make(map[string]string)
				params["service"] = "foo"
				resp, err := doRequest("POST", "/services/foo/reset/", params, nil, resetServiceHandler, nil)
				Convey("Then it should return an error message", func() {
					So(err, ShouldBeNil)
					So(string(resp), ShouldEqual, "Reset only applies to 'in progress' serices, however service 'foo' is on status 'errored")
				})
			})
		})
	})

	Convey("Scenario: generating a uuid", t, func() {
		Convey("Given I do a call to /services/uuid", func() {
			resp, err := doRequest("POST", "/services/uuid/", nil, []byte(`{"id":"foo"}`), createUUIDHandler, nil)

			Convey("It should return the correct encoded uuid", func() {
				So(err, ShouldBeNil)
				So(string(resp), ShouldEqual, `{"uuid":"acbd18db4cc2f85cedef654fccc4a4d8"}`)
			})
		})
	})

	Convey("Scenario: getting a list of services", t, func() {
		Convey("Given services exist on the store", func() {
			foundSubscriber("service.find", `[{"id":"1","name":"test","datacenter_id":1},{"id":"2","name":"test","datacenter_id":2}]`, 2)
			Convey("When I call GET /services/", func() {
				resp, err := doRequest("GET", "/services/", nil, nil, getServicesHandler, nil)

				Convey("It should return the correct set of data", func() {
					var s []OutputService
					So(err, ShouldBeNil)
					err = json.Unmarshal(resp, &s)
					So(err, ShouldBeNil)
					So(len(s), ShouldEqual, 2)
					So(s[0].ID, ShouldEqual, "1")
					So(s[0].Name, ShouldEqual, "test")
					So(s[0].DatacenterID, ShouldEqual, 1)
				})
			})
		})
	})

	Convey("Scenario: getting a single service", t, func() {
		Convey("Given the service exists on the store", func() {
			foundSubscriber("service.find", `[{"id":"1","name":"test","datacenter_id":1},{"id":"2","name":"test","datacenter_id":2}]`, 2)
			Convey("And I call /service/:service on the api", func() {
				var d OutputService
				params := make(map[string]string)
				params["service"] = "1"
				resp, err := doRequest("GET", "/services/:service", params, nil, getServiceHandler, nil)

				Convey("When I'm authenticated as an admin user", func() {
					Convey("Then I should get the existing service", func() {

						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &d)

						So(err, ShouldBeNil)
						So(d.ID, ShouldEqual, "1")
						So(d.Name, ShouldEqual, "test")
					})
				})

				Convey("When the service group matches the authenticated users group", func() {
					ft := generateTestToken(1, "test", false)

					params := make(map[string]string)
					params["service"] = "1"
					resp, err := doRequest("GET", "/services/:service", params, nil, getServiceHandler, ft)

					Convey("Then I should get the existing service", func() {
						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &d)
						So(err, ShouldBeNil)
						So(d.ID, ShouldEqual, "1")
						So(d.Name, ShouldEqual, "test")
					})
				})
			})
		})
	})

	Convey("Scenario: getting a service's builds", t, func() {
		Convey("Given the service exists on the store", func() {
			Convey("And I call /service/:service/builds/ on the api", func() {
				findServiceSubscriber()
				var s []OutputService
				params := make(map[string]string)
				params["service"] = "test"
				resp, err := doRequest("GET", "/services/:service/builds/", params, nil, getServiceBuildsHandler, nil)

				Convey("When I'm authenticated as an admin user", func() {
					Convey("Then I should get the service's builds", func() {
						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &s)

						So(err, ShouldBeNil)
						So(len(s), ShouldEqual, 2)
						So(s[0].ID, ShouldEqual, "1")
						So(s[0].Name, ShouldEqual, "test")
					})
				})

				Convey("When the service group matches the authenticated users group", func() {
					findServiceSubscriber()
					ft := generateTestToken(1, "test", false)

					params := make(map[string]string)
					params["service"] = "test"
					resp, err := doRequest("GET", "/services/:service/builds/", params, nil, getServiceBuildsHandler, ft)

					Convey("Then I should get the service's builds", func() {
						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &s)

						So(len(s), ShouldEqual, 2)
						So(s[0].ID, ShouldEqual, "1")
						So(s[0].Name, ShouldEqual, "test")
					})
				})
			})
		})
	})

	Convey("Scenario: getting a service's build", t, func() {
		Convey("Given the service exists on the store", func() {
			Convey("And I call /service/:service/builds/:build on the api", func() {
				findServiceSubscriber()
				var s OutputService

				params := make(map[string]string)
				params["service"] = "test"
				params["build"] = "1"
				resp, err := doRequest("GET", "/services/:service/builds/:build", params, nil, getServiceBuildHandler, nil)

				Convey("When I'm authenticated as an admin user", func() {
					Convey("Then I should get the existing service", func() {
						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &s)

						So(err, ShouldBeNil)
						So(s.ID, ShouldEqual, "1")
						So(s.Name, ShouldEqual, "test")
					})
				})

				Convey("When the service group matches the authenticated users group", func() {
					findServiceSubscriber()
					ft := generateTestToken(1, "test", false)

					params := make(map[string]string)
					params["service"] = "test"
					params["build"] = "1"
					resp, err := doRequest("GET", "/services/:service/builds/:build", params, nil, getServiceBuildHandler, ft)

					Convey("Then I should get the existing service", func() {
						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &s)
						So(err, ShouldBeNil)
						So(s.ID, ShouldEqual, "1")
						So(s.Name, ShouldEqual, "test")
					})
				})
			})
		})
	})

	Convey("Scenario: searching for services", t, func() {
		Convey("Given the service exists on the store", func() {
			findServiceSubscriber()
			Convey("And I call /service/search/ on the api", func() {
				var s []OutputService
				params := make(map[string]string)
				params["service"] = "1"
				resp, err := doRequest("GET", "/services/search/?name=test", params, nil, searchServicesHandler, nil)<commit_msg>Fix tests<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package main

import (
	"encoding/json"
	"strings"
	"testing"

	. "github.com/smartystreets/goconvey/convey"
)

func TestServices(t *testing.T) {
	testsSetup()
	setup()

	Convey("Scenario: reeting a service", t, func() {
		foundSubscriber("service.set", `"success"`, 1)

		Convey("Given my existing service is in progress", func() {
			foundSubscriber("service.find", `[{"id":"1","name":"test","status":"in_progress"},{"id":"2","name":"test","status":"done"}]`, 1)

			Convey("When I do a call to /services/reset", func() {
				params := make(map[string]string)
				params["service"] = "foo"
				resp, err := doRequest("POST", "/services/foo/reset/", params, nil, resetServiceHandler, nil)
				Convey("Then it should return a success message", func() {
					So(err, ShouldBeNil)
					So(string(resp), ShouldEqual, `success`)
				})
			})
		})

		Convey("Given my existing service is errored", func() {
			foundSubscriber("service.find", `[{"id":"1","name":"test","status":"errored"},{"id":"2","name":"test","status":"done"}]`, 1)

			Convey("When I do a call to /services/reset", func() {
				params := make(map[string]string)
				params["service"] = "foo"
				resp, err := doRequest("POST", "/services/foo/reset/", params, nil, resetServiceHandler, nil)
				Convey("Then it should return an error message", func() {
					So(err, ShouldBeNil)
					So(string(resp), ShouldEqual, "Reset only applies to 'in progress' serices, however service 'foo' is on status 'errored")
				})
			})
		})
	})

	Convey("Scenario: generating a uuid", t, func() {
		Convey("Given I do a call to /services/uuid", func() {
			resp, err := doRequest("POST", "/services/uuid/", nil, []byte(`{"id":"foo"}`), createUUIDHandler, nil)

			Convey("It should return the correct encoded uuid", func() {
				So(err, ShouldBeNil)
				So(string(resp), ShouldEqual, `{"uuid":"acbd18db4cc2f85cedef654fccc4a4d8"}`)
			})
		})
	})

	Convey("Scenario: getting a list of services", t, func() {
		Convey("Given services exist on the store", func() {
			foundSubscriber("service.find", `[{"id":"1","name":"test","datacenter_id":1},{"id":"2","name":"test","datacenter_id":2}]`, 2)
			Convey("When I call GET /services/", func() {
				resp, err := doRequest("GET", "/services/", nil, nil, getServicesHandler, nil)

				Convey("It should return the correct set of data", func() {
					var s []OutputService
					So(err, ShouldBeNil)
					err = json.Unmarshal(resp, &s)
					So(err, ShouldBeNil)
					So(len(s), ShouldEqual, 2)
					So(s[0].ID, ShouldEqual, "1")
					So(s[0].Name, ShouldEqual, "test")
					So(s[0].DatacenterID, ShouldEqual, 1)
				})
			})
		})
	})

	Convey("Scenario: getting a single service", t, func() {
		Convey("Given the service exists on the store", func() {
			foundSubscriber("service.find", `[{"id":"1","name":"test","datacenter_id":1},{"id":"2","name":"test","datacenter_id":2}]`, 2)
			Convey("And I call /service/:service on the api", func() {
				var d OutputService
				params := make(map[string]string)
				params["service"] = "1"
				resp, err := doRequest("GET", "/services/:service", params, nil, getServiceHandler, nil)

				Convey("When I'm authenticated as an admin user", func() {
					Convey("Then I should get the existing service", func() {

						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &d)

						So(err, ShouldBeNil)
						So(d.ID, ShouldEqual, "1")
						So(d.Name, ShouldEqual, "test")
					})
				})

				Convey("When the service group matches the authenticated users group", func() {
					ft := generateTestToken(1, "test", false)

					params := make(map[string]string)
					params["service"] = "1"
					resp, err := doRequest("GET", "/services/:service", params, nil, getServiceHandler, ft)

					Convey("Then I should get the existing service", func() {
						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &d)
						So(err, ShouldBeNil)
						So(d.ID, ShouldEqual, "1")
						So(d.Name, ShouldEqual, "test")
					})
				})
			})
		})
	})

	Convey("Scenario: getting a service's builds", t, func() {
		Convey("Given the service exists on the store", func() {
			Convey("And I call /service/:service/builds/ on the api", func() {
				findServiceSubscriber()
				var s []OutputService
				params := make(map[string]string)
				params["service"] = "test"
				resp, err := doRequest("GET", "/services/:service/builds/", params, nil, getServiceBuildsHandler, nil)

				Convey("When I'm authenticated as an admin user", func() {
					Convey("Then I should get the service's builds", func() {
						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &s)

						So(err, ShouldBeNil)
						So(len(s), ShouldEqual, 2)
						So(s[0].ID, ShouldEqual, "1")
						So(s[0].Name, ShouldEqual, "test")
					})
				})

				Convey("When the service group matches the authenticated users group", func() {
					findServiceSubscriber()
					ft := generateTestToken(1, "test", false)

					params := make(map[string]string)
					params["service"] = "test"
					resp, err := doRequest("GET", "/services/:service/builds/", params, nil, getServiceBuildsHandler, ft)

					Convey("Then I should get the service's builds", func() {
						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &s)

						So(len(s), ShouldEqual, 2)
						So(s[0].ID, ShouldEqual, "1")
						So(s[0].Name, ShouldEqual, "test")
					})
				})
			})
		})
	})

	Convey("Scenario: getting a service's build", t, func() {
		Convey("Given the service exists on the store", func() {
			Convey("And I call /service/:service/builds/:build on the api", func() {
				findServiceSubscriber()
				var s OutputService

				params := make(map[string]string)
				params["service"] = "test"
				params["id"] = "1"
				resp, err := doRequest("GET", "/services/:service/builds/:build", params, nil, getServiceBuildHandler, nil)

				Convey("When I'm authenticated as an admin user", func() {
					Convey("Then I should get the existing service", func() {
						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &s)

						So(err, ShouldBeNil)
						So(s.ID, ShouldEqual, "1")
						So(s.Name, ShouldEqual, "test")
					})
				})

				Convey("When the service group matches the authenticated users group", func() {
					findServiceSubscriber()
					ft := generateTestToken(1, "test", false)

					params := make(map[string]string)
					params["service"] = "test"
					params["id"] = "1"
					resp, err := doRequest("GET", "/services/:service/builds/:build", params, nil, getServiceBuildHandler, ft)

					Convey("Then I should get the existing service", func() {
						So(err, ShouldBeNil)
						err = json.Unmarshal(resp, &s)
						So(err, ShouldBeNil)
						So(s.ID, ShouldEqual, "1")
						So(s.Name, ShouldEqual, "test")
					})
				})
			})
		})
	})

	Convey("Scenario: searching for services", t, func() {
		Convey("Given the service exists on the store", func() {
			findServiceSubscriber()
			Convey("And I call /service/search/ on the api", func() {
				var s []OutputService
				params := make(map[string]string)
				params["service"] = "1"
				resp, err := doRequest("GET", "/services/search/?name=test", params, nil, searchServicesHandler, nil)
{'fraction_non_alphanumeric': 0.17377717391304348, 'fraction_numerical': 0.008016304347826088, 'mean_word_length': 3.629245283018868, 'pattern_counts': {'":': 52, '<': 3, '<?xml version=': 0, '>': 3, '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': '5727278', 'n_tokens_mistral': 5928, 'n_tokens_neox': 4786, 'n_words': 1298}