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

feat: Create new test block #126

Merged
merged 11 commits into from
Dec 29, 2023
Binary file added __tests__/data/java_tests_sample.zip
Binary file not shown.
67 changes: 66 additions & 1 deletion __tests__/integration/laboratories_utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package integration

import "net/http"
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"os"
)

func CreateLaboratory(cookie *http.Cookie, payload map[string]interface{}) (response map[string]interface{}, statusCode int) {
w, r := PrepareRequest("POST", "/api/v1/laboratories", payload)
Expand Down Expand Up @@ -37,3 +44,61 @@ func CreateMarkdownBlock(cookie *http.Cookie, laboratoryUUID string) (response m
jsonResponse := ParseJsonResponse(w.Body)
return jsonResponse, w.Code
}

type CreateTestBlockUtilsDTO struct {
laboratoryUUID string
languageUUID string
blockName string
cookie *http.Cookie
testFile *os.File
}

func CreateTestBlock(dto *CreateTestBlockUtilsDTO) (response map[string]interface{}, statusCode int) {
// Create the request body
var body bytes.Buffer

// Create the multipart form
writer := multipart.NewWriter(&body)

// Add the file to the form
h := make(textproto.MIMEHeader)
h.Set("Content-Disposition", "form-data; name=\"test_archive\"; filename=\"test.zip\"")
h.Set("Content-Type", "application/zip")

fileWriter, err := writer.CreatePart(h)
if err != nil {
panic(err)
}

_, err = io.Copy(fileWriter, dto.testFile)
if err != nil {
panic(err)
}

// Add the text fields to the form
err = writer.WriteField("block_name", dto.blockName)
if err != nil {
panic(err)
}

err = writer.WriteField("language_uuid", dto.languageUUID)
if err != nil {
panic(err)
}

// Close the multipart form
err = writer.Close()
if err != nil {
panic(err)
}

// Create the request
w, r := PrepareMultipartRequest("POST", "/api/v1/laboratories/test_blocks/"+dto.laboratoryUUID, &body)
r.AddCookie(dto.cookie)
r.Header.Set("Content-Type", writer.FormDataContentType())

// Send the request
router.ServeHTTP(w, r)
jsonResponse := ParseJsonResponse(w.Body)
return jsonResponse, w.Code
}
55 changes: 55 additions & 0 deletions __tests__/integration/laboratorires_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package integration

import (
"net/http"
"os"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -279,3 +280,57 @@ func TestCreateMarkdownBlock(t *testing.T) {
c.Equal("", block["content"])
c.EqualValues(1, block["index"])
}

func TestCreateTestBlock(t *testing.T) {
c := require.New(t)

// Login as a teacher
w, r := PrepareRequest("POST", "/api/v1/session/login", map[string]interface{}{
"email": registeredTeacherEmail,
"password": registeredTeacherPass,
})
router.ServeHTTP(w, r)
cookie := w.Result().Cookies()[0]

// Create a course
courseUUID, status := CreateCourse("Create test block test - course")
c.Equal(http.StatusCreated, status)

// Create a laboratory
laboratoryCreationResponse, status := CreateLaboratory(cookie, map[string]interface{}{
"name": "Create test block test - laboratory",
"course_uuid": courseUUID,
"opening_date": "2023-12-01T08:00",
"due_date": "3023-12-01T00:00",
})
laboratoryUUID := laboratoryCreationResponse["uuid"].(string)
c.Equal(http.StatusCreated, status)

// Get the supported languages
languagesResponse, status := GetSupportedLanguages(cookie)
c.Equal(http.StatusOK, status)

languages := languagesResponse["languages"].([]interface{})
c.Greater(len(languages), 0)

firstLanguage := languages[0].(map[string]interface{})
firstLanguageUUID := firstLanguage["uuid"].(string)

// Open `.zip` file from the data folder
TEST_FILE_PATH := "../data/java_tests_sample.zip"
zipFile, err := os.Open(TEST_FILE_PATH)
c.Nil(err)

// Send the request
response, _ := CreateTestBlock(&CreateTestBlockUtilsDTO{
laboratoryUUID: laboratoryUUID,
languageUUID: firstLanguageUUID,
blockName: "Create test block test - block",
cookie: cookie,
testFile: zipFile,
})

// Validate the response
// c.Equal(http.StatusCreated, status)
c.Contains(response, "uuid")
}
10 changes: 10 additions & 0 deletions __tests__/integration/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ func PrepareRequest(method, endpoint string, payload interface{}) (*httptest.Res
return w, req
}

func PrepareMultipartRequest(method, endpoint string, body *bytes.Buffer) (*httptest.ResponseRecorder, *http.Request) {
var req *http.Request

req, _ = http.NewRequest(method, endpoint, body)
req.Header.Set("Content-Type", "multipart/form-data")

w := httptest.NewRecorder()
return w, req
}

func ParseJsonResponse(buffer *bytes.Buffer) map[string]interface{} {
var response map[string]interface{}
json.Unmarshal(buffer.Bytes(), &response)
Expand Down
156 changes: 156 additions & 0 deletions docs/insomnia/collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
{
"_type": "export",
"__export_format": 4,
"__export_date": "2023-12-29T19:18:12.594Z",
"__export_source": "insomnia.desktop.app:v8.5.1",
"resources": [
{
"_id": "req_1f5b243091ed48d19b56b9e9f1b8b60a",
"parentId": "fld_0f115af031f64a478dc8707d5aba1048",
"modified": 1703877462736,
"created": 1703867533491,
"url": "{{ _.BASE_URL }}/laboratories/test_blocks/77ab37da-580e-44d6-86d3-d3334cb24b12",
"name": "create-test-block",
"description": "",
"method": "POST",
"body": {
"mimeType": "multipart/form-data",
"params": [
{
"id": "pair_a735916e2a8a49779538d59b17038494",
"name": "language_uuid",
"value": "19c11b28-3d06-4c54-8946-6af8a28e8b07",
"description": ""
},
{
"id": "pair_08961c834b7f4eba9b58187135414cd6",
"name": "block_name",
"value": "Calculator operations",
"description": ""
},
{
"id": "pair_a3253709f396429dbbdb03f522270092",
"name": "test_archive",
"value": "",
"description": "",
"type": "file",
"fileName": "/home/pacq/IdeaProjects/java.zip"
}
]
},
"parameters": [],
"headers": [
{ "name": "Content-Type", "value": "multipart/form-data" },
{ "name": "User-Agent", "value": "insomnia/8.5.1" }
],
"authentication": {},
"metaSortKey": -1703867533491,
"isPrivate": false,
"settingStoreCookies": true,
"settingSendCookies": true,
"settingDisableRenderRequestBody": false,
"settingEncodeUrl": true,
"settingRebuildPath": true,
"settingFollowRedirects": "global",
"_type": "request"
},
{
"_id": "fld_0f115af031f64a478dc8707d5aba1048",
"parentId": "wrk_015f06b0727b4d81af80c31f9fc4f68f",
"modified": 1703867530994,
"created": 1703867530994,
"name": "laboratories",
"description": "",
"environment": {},
"environmentPropertyOrder": null,
"metaSortKey": -1703867530994,
"_type": "request_group"
},
{
"_id": "wrk_015f06b0727b4d81af80c31f9fc4f68f",
"parentId": null,
"modified": 1703867428111,
"created": 1703867428111,
"name": "UPB Codelabs Multipart Form Request",
"description": "",
"scope": "collection",
"_type": "workspace"
},
{
"_id": "req_81c24080612643dea636b43cba6a5c72",
"parentId": "fld_e99fc9329e1e4e9ea4aad5ac37ff965c",
"modified": 1703867500413,
"created": 1703867446159,
"url": "{{ _.BASE_URL }}/session/login",
"name": "login-as-teacher",
"description": "",
"method": "POST",
"body": {
"mimeType": "application/json",
"text": "{\n \"email\": \"[email protected]\", \n \"password\": \"upbbbga2023*/\"\n}"
},
"parameters": [],
"headers": [
{ "name": "Content-Type", "value": "application/json" },
{ "name": "User-Agent", "value": "insomnia/8.5.1" }
],
"authentication": {},
"metaSortKey": -1703867446159,
"isPrivate": false,
"settingStoreCookies": true,
"settingSendCookies": true,
"settingDisableRenderRequestBody": false,
"settingEncodeUrl": true,
"settingRebuildPath": true,
"settingFollowRedirects": "global",
"_type": "request"
},
{
"_id": "fld_e99fc9329e1e4e9ea4aad5ac37ff965c",
"parentId": "wrk_015f06b0727b4d81af80c31f9fc4f68f",
"modified": 1703867444303,
"created": 1703867444303,
"name": "session",
"description": "",
"environment": {},
"environmentPropertyOrder": null,
"metaSortKey": -1703867444303,
"_type": "request_group"
},
{
"_id": "env_5128b6531490f5b58316ee3db66aec4f90c368ff",
"parentId": "wrk_015f06b0727b4d81af80c31f9fc4f68f",
"modified": 1703867471567,
"created": 1703867428113,
"name": "Base Environment",
"data": { "BASE_URL": "http://127.0.0.1:8080/api/v1" },
"dataPropertyOrder": { "&": ["BASE_URL"] },
"color": null,
"isPrivate": false,
"metaSortKey": 1703867428113,
"_type": "environment"
},
{
"_id": "jar_5128b6531490f5b58316ee3db66aec4f90c368ff",
"parentId": "wrk_015f06b0727b4d81af80c31f9fc4f68f",
"modified": 1703867520274,
"created": 1703867428114,
"name": "Default Jar",
"cookies": [
{
"key": "session",
"value": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1dWlkIjoiNWM2NjY5ZDgtODFiYi00NGEyLTk4YTMtMDRhNTBmY2I1NWU1Iiwicm9sZSI6InRlYWNoZXIiLCJpc3MiOiJjb2RlbGFicyIsImV4cCI6MTcwMzg4OTEyMCwibmJmIjoxNzAzODY3NTIwLCJpYXQiOjE3MDM4Njc1MjB9.XCEIBKwjjNLja0B1eYNlZIc51I5f5nHPic3UWLUC-lM",
"maxAge": 21600,
"domain": "127.0.0.1",
"path": "/",
"httpOnly": true,
"hostOnly": true,
"creation": "2023-12-29T16:32:00.273Z",
"lastAccessed": "2023-12-29T16:32:00.273Z",
"id": "f3827bc3-8097-4fa7-8dfa-d3c3f7e032f9"
}
],
"_type": "cookie_jar"
}
]
}
4 changes: 2 additions & 2 deletions docs/openapi/spec.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1849,13 +1849,13 @@ components:
create_test_block_req:
type: object
properties:
name:
block_name:
type: string
example: "Test métodos de la lista simplemente enlazada"
language_uuid:
type: string
example: "a7c4c843-c3ef-4083-8255-927aea3af77f"
file:
test_archive:
type: string
format: binary # A `.zip` archive

Expand Down
4 changes: 4 additions & 0 deletions src/blocks/domain/definitions/blocks_repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package definitions

import "mime/multipart"

type BlockRepository interface {
UpdateMarkdownBlockContent(blockUUID string, content string) (err error)
DoesTeacherOwnsMarkdownBlock(teacherUUID string, blockUUID string) (bool, error)

SaveTestsArchive(file *multipart.File) (uuid string, err error)
}
Loading