package user import ( "bytes" "encoding/json" "io/ioutil" "net/http/httptest" "testing" "git.samuelpua.com/telboon/ktm-train-bot/backend/internal/common" ) func TestSetProfile(t *testing.T) { db := common.TestDBInit() defer common.DestroyTestingDB(db) db.AutoMigrate(&User{}) db.AutoMigrate(&Profile{}) db.AutoMigrate(&Session{}) t.Setenv("ALLOW_REGISTRATION", "true") t.Setenv("COOKIE_STRING", "supercustomcookie") router := UserRoutes(db) testCases := []struct { ktmTrainUsername string ktmTrainPassword string ktmTrainCreditCardType string ktmTrainCreditCard string ktmTrainCreditCardExpiry string ktmTrainCreditCardCVV string statusCode int cookieEnabled bool }{ { ktmTrainUsername: "test", ktmTrainPassword: "test", ktmTrainCreditCardType: "Visa", ktmTrainCreditCard: "1234567890123456", ktmTrainCreditCardExpiry: "05/2025", ktmTrainCreditCardCVV: "123", statusCode: 200, cookieEnabled: true, }, { ktmTrainUsername: "", ktmTrainPassword: "", ktmTrainCreditCardType: "", ktmTrainCreditCard: "", ktmTrainCreditCardExpiry: "", ktmTrainCreditCardCVV: "", statusCode: 200, cookieEnabled: true, }, { ktmTrainUsername: "test", ktmTrainPassword: "test", ktmTrainCreditCardType: "Visa", ktmTrainCreditCard: "1234567890123456", ktmTrainCreditCardExpiry: "05/2025", ktmTrainCreditCardCVV: "123", statusCode: 500, cookieEnabled: false, }, } // Register user rr := httptest.NewRecorder() currBody := struct { Username string `json:"username"` Password string `json:"password"` }{ Username: "testusername", Password: "testpassword", } reqBody, err := json.Marshal(currBody) if err != nil { t.Errorf("Error creating a new request body: %v", err) } reqBodyReader := bytes.NewReader(reqBody) req := httptest.NewRequest("POST", "/register", reqBodyReader) router.ServeHTTP(rr, req) // Check registration results if rr.Code != 201 { t.Errorf("Expected status code %d, got %d", 201, rr.Code) } // Login to get cookie rr = httptest.NewRecorder() currBodyLogin := struct { Username string `json:"username"` Password string `json:"password"` }{ Username: "testusername", Password: "testpassword", } reqBody, err = json.Marshal(currBodyLogin) if err != nil { t.Errorf("Error creating a new request body: %v", err) } reqBodyReader = bytes.NewReader(reqBody) req = httptest.NewRequest("POST", "/login", reqBodyReader) router.ServeHTTP(rr, req) for _, currentTestCase := range testCases { // Start checking Profile rrProfile := httptest.NewRecorder() currBody := struct { KtmTrainUsername string `json:"ktmTrainUsername"` KtmTrainPassword string `json:"ktmTrainPassword"` KtmTrainCreditCardType string `json:"ktmTrainCreditCardType"` KtmTrainCreditCard string `json:"ktmTrainCreditCard"` KtmTrainCreditCardExpiry string `json:"ktmTrainCreditCardExpiry"` KtmTrainCreditCardCVV string `json:"ktmTrainCreditCardCVV"` }{ KtmTrainUsername: currentTestCase.ktmTrainUsername, KtmTrainPassword: currentTestCase.ktmTrainPassword, KtmTrainCreditCardType: currentTestCase.ktmTrainCreditCardType, KtmTrainCreditCard: currentTestCase.ktmTrainCreditCard, KtmTrainCreditCardExpiry: currentTestCase.ktmTrainCreditCardExpiry, KtmTrainCreditCardCVV: currentTestCase.ktmTrainCreditCardCVV, } reqBody, err = json.Marshal(currBody) if err != nil { t.Errorf("Error creating a new request body: %v", err) } reqBodyReader = bytes.NewReader(reqBody) req = httptest.NewRequest("PUT", "/profile", reqBodyReader) if currentTestCase.cookieEnabled { req.AddCookie(rr.Result().Cookies()[0]) } router.ServeHTTP(rrProfile, req) // Check Profile results if rrProfile.Code != currentTestCase.statusCode { t.Errorf("Expected status code %d, got %d", currentTestCase.statusCode, rrProfile.Code) } if currentTestCase.statusCode == 200 { var resultsObj map[string]any resBodyBytes, err := ioutil.ReadAll(rrProfile.Body) if err != nil { t.Errorf("Error reading response body: %v", err) } err = json.Unmarshal(resBodyBytes, &resultsObj) if err != nil { t.Errorf("Error unmarshalling response body: %v", err) } resultsProfile := resultsObj["profile"].(map[string]any) if resultsObj["username"] != "testusername" { t.Errorf("Expected username %s, got %s", "testusername", resultsObj["username"]) } if resultsProfile["ktmTrainUsername"] != currentTestCase.ktmTrainUsername { t.Errorf("Expected ktmTrainUsername %s, got %s", currentTestCase.ktmTrainUsername, resultsProfile["ktmTrainUsername"]) } if resultsProfile["ktmTrainPassword"] != currentTestCase.ktmTrainPassword { t.Errorf("Expected ktmTrainPassword %s, got %s", currentTestCase.ktmTrainPassword, resultsProfile["ktmTrainPassword"]) } if resultsProfile["ktmTrainCreditCardType"] != currentTestCase.ktmTrainCreditCardType { t.Errorf("Expected ktmTrainCreditCardType %s, got %s", currentTestCase.ktmTrainCreditCardType, resultsProfile["ktmTrainCreditCardType"]) } if resultsProfile["ktmTrainCreditCard"] != currentTestCase.ktmTrainCreditCard { t.Errorf("Expected ktmTrainCreditCard %s, got %s", currentTestCase.ktmTrainCreditCard, resultsProfile["ktmTrainCreditCard"]) } if resultsProfile["ktmTrainCreditCardExpiry"] != currentTestCase.ktmTrainCreditCardExpiry { t.Errorf("Expected ktmTrainCreditCardExpiry %s, got %s", currentTestCase.ktmTrainCreditCardExpiry, resultsProfile["ktmTrainCreditCardExpiry"]) } if resultsProfile["ktmTrainCreditCardCVV"] != currentTestCase.ktmTrainCreditCardCVV { t.Errorf("Expected ktmTrainCreditCardCVV %s, got %s", currentTestCase.ktmTrainCreditCardCVV, resultsProfile["ktmTrainCreditCardCVV"]) } } } }