Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Cases/Unit test for the /generate-presigned-url endpoint #22

Closed
uzaxirr opened this issue Jan 1, 2023 · 6 comments
Closed

Test Cases/Unit test for the /generate-presigned-url endpoint #22

uzaxirr opened this issue Jan 1, 2023 · 6 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@uzaxirr
Copy link
Member

uzaxirr commented Jan 1, 2023

It is desired to have test cases for the /generate-presigned-url endpoint which gives a pre-signed url and videoID as output. the s3 SDK is needed to be mocked.

@uzaxirr
Copy link
Member Author

uzaxirr commented Jan 1, 2023

Here is a snippet that might be useful

// mockS3Client is a mock object that implements the S3 client interface
type mockS3Client struct {
	mock.Mock
}

// PutObjectRequest is a mock function that implements the S3 client's PutObjectRequest method
func (m *mockS3Client) PutObjectRequest(input *s3.PutObjectInput) (*s3.PutObjectOutput, error) {
	args := m.Called(input)
	return args.Get(0).(*s3.PutObjectOutput), args.Error(1)
}

// Presign is a mock function that implements the S3 client's Presign method
func (m *mockS3Client) Presign(input *http.Request, expiration time.Duration) (string, error) {
	args := m.Called(input, expiration)
	return args.String(0), args.Error(1)
}

// AbortMultipartUpload is a mock function that implements the S3 client's AbortMultipartUpload method
func (m *mockS3Client) AbortMultipartUpload(input *s3.AbortMultipartUploadInput) (*s3.AbortMultipartUploadOutput, error) {
	args := m.Called(input)
	return args.Get(0).(*s3.AbortMultipartUploadOutput), args.Error(1)
}

// AbortMultipartUploadRequest is a mock function that implements the S3 client's AbortMultipartUploadRequest method
func (m *mockS3Client) AbortMultipartUploadRequest(input *s3.AbortMultipartUploadInput) (*request.Request, *s3.AbortMultipartUploadOutput) {
	var args = m.Called(input)
	return args.Get(0).(*request.Request), args.Get(1).(*s3.AbortMultipartUploadOutput)
}

func setupRoutes(s3Client *mockS3Client) *gin.Engine {
	router := gin.Default()

	// Set up the route for the GeneratePresignedURL function
	router.GET("/presigned-url", func(c *gin.Context) {
		GeneratePresignedURL(
			c,
			s3Client,
		)
	})

	return router
}

func TestGeneratePresignedURL(t *testing.T) {
	// Create a mock object for the S3 service client
	mockS3Client := &mockS3Client{}

	// Define a function that will be used to mock the Presign method
	mockS3Client.On("Presign", mock.Anything).Return("https://my-presigned-url.com", nil)

	// Set up a test HTTP server and routes using the mock S3 client
	server := httptest.NewServer(setupRoutes(mockS3Client))
	defer server.Close()

	// Create a test HTTP request with the fileName and extension query params
	req, err := http.NewRequest("GET", server.URL+"/presigned-url?fileName=test.mp4", nil)
	if err != nil {
		t.Fatalf("Error creating HTTP request: %s", err)
	}

	// Make the HTTP request to the GeneratePresignedURL function
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		t.Fatalf("Error making HTTP request: %s", err)
	}

	// Assert that the response status is 200 OK
	assert.Equal(t, http.StatusOK, res.StatusCode)

	// Read the response body
	resBody, err := ioutil.ReadAll(res.Body)
	if err != nil {
		t.Fatalf("Error reading response body: %s", err)
	}

	// Unmarshal the response body into a map
	var data map[string]interface{}
	err = json.Unmarshal(resBody, &data)
	if err != nil {
		t.Fatalf("Error unmarshalling response body: %s", err)
	}

	// Check that the response includes the expected pre-signed URL and video ID
	if data["preSignedURL"] != "https://my-presigned-url.com" {
		t.Errorf("Expected pre-signed URL 'https://my-presigned-url.com', got %s", data["preSignedURL"])
	}
	if data["videoID"] == nil {
		t.Error("Expected video ID in response, got nil")
	}
}

@uzaxirr uzaxirr added the help wanted Extra attention is needed label Jan 1, 2023
@zarszz
Copy link

zarszz commented Jan 8, 2023

hi, can I work on this PR?

@uzaxirr
Copy link
Member Author

uzaxirr commented Jan 8, 2023

hi, can I work on this PR?

Hey,Sure you can. Assigning it to you right away!!

@uzaxirr
Copy link
Member Author

uzaxirr commented Jan 12, 2023

Hey @zarszz
Any updates on this one, Need any help?

@uzaxirr
Copy link
Member Author

uzaxirr commented Jan 15, 2023

unassigning due to inactivity

@YOGESH256
Copy link

Can I work on this issue? I'm new to Go and have been following this repository for about a month. I would like to contribute.

@abskrj abskrj closed this as completed Nov 6, 2023
@abskrj abskrj unpinned this issue Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants