Add skeleton for saving and loading sessions

This commit is contained in:
Alessio 2023-01-08 15:40:17 -05:00
parent 390c831541
commit ab9e67bbb1
2 changed files with 50 additions and 0 deletions

22
persistence/session.go Normal file
View File

@ -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")
}

View File

@ -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)
}
}