From ab9e67bbb1b26c76d1b07bb95b59315b38b93a46 Mon Sep 17 00:00:00 2001 From: Alessio Date: Sun, 8 Jan 2023 15:40:17 -0500 Subject: [PATCH] Add skeleton for saving and loading sessions --- persistence/session.go | 22 ++++++++++++++++++++++ persistence/session_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 persistence/session.go create mode 100644 persistence/session_test.go diff --git a/persistence/session.go b/persistence/session.go new file mode 100644 index 0000000..6d2c020 --- /dev/null +++ b/persistence/session.go @@ -0,0 +1,22 @@ +package persistence + +import ( + "offline_twitter/scraper" +) + +func (p Profile) SaveSession(api scraper.API) { + // TODO session-saving:1 + // - To understand what's going on here, look at the `MarshalJSON` function in `scraper/api_request_utils.go`, + // and the output of `git show 390c83154117aa2a339a83f05820fb904a32298e`. + // - use `json.Marshal` on the API object and write the resulting bytes to like "[api.UserHandle].session" or something + // - use `os.WriteFile` to write the file + panic("TODO") +} + +func (p Profile) LoadSession(userhandle scraper.UserHandle) scraper.API { + // TODO session-saving:2 + // - use `os.ReadFile` to read "[userhandle].session" + // - create a variable of type scraper.API + // - use `json.Unmarshal` to load the file contents into the new API variable + panic("TODO") +} diff --git a/persistence/session_test.go b/persistence/session_test.go new file mode 100644 index 0000000..5b706a2 --- /dev/null +++ b/persistence/session_test.go @@ -0,0 +1,28 @@ +package persistence_test + +import ( + "github.com/go-test/deep" + "offline_twitter/persistence" + "offline_twitter/scraper" + "testing" +) + +// Save and load an API session; it should come back the same +func TestSaveAndLoadAuthenticatedSession(t *testing.T) { + assert := assert.New(t) + profile_path := "test_profiles/TestSession" + profile := create_or_load_profile(profile_path) + + api := scraper.API{ + // TODO session-saving + // - Fill out some fields here like Cookies and CSRFToken and UserHandle + } + + // Save and load the session; it should come back the same + profile.SaveSession(api) + new_api = profile.LoadSession(api.UserHandle) + + if diff := deep.Equal(api, new_api); diff != nil { + t.Errorf(diff) + } +}