diff --git a/server/README.Docker.md b/server/README.Docker.md deleted file mode 100644 index b7cfe70..0000000 --- a/server/README.Docker.md +++ /dev/null @@ -1,22 +0,0 @@ -### Building and running your application - -When you're ready, start your application by running: -`docker compose up --build`. - -Your application will be available at http://localhost:3000. - -### Deploying your application to the cloud - -First, build your image, e.g.: `docker build -t myapp .`. -If your cloud uses a different CPU architecture than your development -machine (e.g., you are on a Mac M1 and your cloud provider is amd64), -you'll want to build the image for that platform, e.g.: -`docker build --platform=linux/amd64 -t myapp .`. - -Then, push it to your registry, e.g. `docker push myregistry.com/myapp`. - -Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/) -docs for more detail on building and pushing. - -### References -* [Docker's Go guide](https://docs.docker.com/language/golang/) \ No newline at end of file diff --git a/server/github/github_test.go b/server/github/github_test.go new file mode 100644 index 0000000..fce894e --- /dev/null +++ b/server/github/github_test.go @@ -0,0 +1,84 @@ +package github + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "os" + "testing" + "time" + + "github.com/joho/godotenv" + "github.com/stretchr/testify/assert" +) + +func MockloadAPIKey() (string, error) { + _ = godotenv.Load() + apiKey := os.Getenv("GITHUB_APIKEY") + if apiKey == "" { + return "", fmt.Errorf("API key not found in environment variables") + } + + return apiKey, nil +} +func TestGetRecentIssuesByLanguage(t *testing.T) { + // Load environment variables from .env file + apiKey, err := MockloadAPIKey() + if err != nil { + // Handle the error (e.g., log it and exit) + fmt.Printf("Error loading API key: %v\n", err) + os.Exit(1) + } + + // Mock server to simulate GitHub API responses + mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "token "+apiKey, r.Header.Get("Authorization")) + + repositories := []map[string]interface{}{ + { + "name": "repo1", + "description": "description1", + "open_issues_count": 5, + "html_url": "https://github.com/user/repo1", + "pushed_at": time.Now().AddDate(0, -3, 0).Format(time.RFC3339), + "stargazers_count": 10, + "language": "Go", + }, + { + "name": "repo2", + "description": "description2", + "open_issues_count": 2, + "html_url": "https://github.com/user/repo2", + "pushed_at": time.Now().AddDate(0, -1, 0).Format(time.RFC3339), + "stargazers_count": 20, + "language": "Go", + }, + } + + response := struct { + Items []map[string]interface{} `json:"items"` + }{Items: repositories} + + json.NewEncoder(w).Encode(response) + })) + defer mockServer.Close() + + // Override the baseURL with the mock server URL + + t.Run("successful response", func(t *testing.T) { + repos, err := GetRecentIssuesByLanguage("Go") + assert.NoError(t, err) + assert.Len(t, repos, 2) + assert.Equal(t, "repo1", repos[0].Name) + assert.Equal(t, "repo2", repos[1].Name) + }) + + t.Run("API key missing", func(t *testing.T) { + os.Setenv("GITHUB_APIKEY", "") + _, err := GetRecentIssuesByLanguage("Go") + assert.Error(t, err) + assert.Equal(t, "API key not found in environment variables", err.Error()) + os.Setenv("GITHUB_APIKEY", apiKey) // Reset the API key for other tests + }) +} diff --git a/server/go.mod b/server/go.mod index b2d967e..68c44d2 100644 --- a/server/go.mod +++ b/server/go.mod @@ -9,4 +9,13 @@ require ( github.com/rs/cors v1.11.0 ) -require github.com/joho/godotenv v1.5.1 +require ( + github.com/joho/godotenv v1.5.1 + github.com/stretchr/testify v1.9.0 +) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/server/go.sum b/server/go.sum index cd78ac1..bfcd181 100644 --- a/server/go.sum +++ b/server/go.sum @@ -1,8 +1,18 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE= github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw= github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw= github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rs/cors v1.11.0 h1:0B9GE/r9Bc2UxRMMtymBkHTenPkHDv0CW4Y98GBY+po= github.com/rs/cors v1.11.0/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=