Skip to content

Commit

Permalink
fix: Always reading trust token from file
Browse files Browse the repository at this point in the history
  • Loading branch information
steilerDev committed Aug 15, 2023
1 parent 2a7fa69 commit 9c14564
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 42 deletions.
2 changes: 1 addition & 1 deletion app/src/lib/resources/resource-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class ResourceManager {
*/
get trustToken(): string | undefined {
const resourceFile = this._readResourceFile();
this._resources.trustToken = resourceFile.trustToken
this._resources.trustToken = resourceFile.trustToken;

return this._resources.trustToken;
}
Expand Down
2 changes: 1 addition & 1 deletion app/test/_helpers/_general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function prepareResources(initiate: boolean = true, appOptions: iCPSAppOp

instances.manager._writeResourceFile = ResourceManager.prototype._writeResourceFile as jest.Mock<typeof ResourceManager.prototype._writeResourceFile>;

instances.manager._readResourceFile = ResourceManager.prototype._readResourceFile as jest.Mock<typeof ResourceManager.prototype._readResourceFile>;;
instances.manager._readResourceFile = ResourceManager.prototype._readResourceFile as jest.Mock<typeof ResourceManager.prototype._readResourceFile>;

ResourceManager.prototype._writeResourceFile = originalWriteResourceFile;
ResourceManager.prototype._readResourceFile = originalReadResourceFile;
Expand Down
8 changes: 4 additions & 4 deletions app/test/unit/icloud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ describe.each([
mockedResourceManager._readResourceFile
.mockReturnValue({
libraryVersion: 1,
trustToken: Config.trustToken
})
trustToken: Config.trustToken,
});
mockedNetworkManager.photosUrl = photosDomain;
});

Expand Down Expand Up @@ -160,8 +160,8 @@ describe.each([
mockedResourceManager._readResourceFile
.mockReturnValue({
libraryVersion: 1,
trustToken: undefined
})
trustToken: undefined,
});

// ICloud.authenticate returns ready promise. Need to modify in order to resolve at the end of the test
icloud.ready = new Promise<void>((resolve, _reject) => resolve());
Expand Down
8 changes: 3 additions & 5 deletions app/test/unit/resources.network-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

import {test, expect, describe, beforeEach, beforeAll} from '@jest/globals';
import {test, expect, describe, beforeAll} from '@jest/globals';
import {Header, HeaderJar} from "../../src/lib/resources/network-manager";
import axios from 'axios';
import {Cookie} from 'tough-cookie';
import {addHoursToCurrentDate, getDateInThePast, prepareResources} from '../_helpers/_general';

describe(`HeaderJar`, () => {

test(`Should initialize`, () => {
const axiosInstance = axios.create();
const headerJar = new HeaderJar(axiosInstance);
Expand Down Expand Up @@ -83,10 +82,9 @@ describe(`HeaderJar`, () => {
},
},
])(`$desc`, ({headers, injectedHeaders}) => {

beforeAll(() => {
prepareResources() // Only setting up for access to logger
})
prepareResources(); // Only setting up for access to logger
});

test.each([
{
Expand Down
62 changes: 31 additions & 31 deletions app/test/unit/resources.resource-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe(`ResourceManager`, () => {
ResourceManager.prototype._writeResourceFile = jest.fn<typeof ResourceManager.prototype._writeResourceFile>()
.mockReturnValue();

ResourceManager.prototype._readResourceFile = jest.fn<typeof ResourceManager.prototype._readResourceFile>()
ResourceManager.prototype._readResourceFile = jest.fn<typeof ResourceManager.prototype._readResourceFile>();
});

afterEach(() => {
Expand All @@ -38,8 +38,8 @@ describe(`ResourceManager`, () => {
expect(resourceManager).toBeInstanceOf(ResourceManager);
expect(resourceManager._readResourceFile).toHaveBeenCalledTimes(1);
expect(resourceManager._writeResourceFile).toHaveBeenCalledTimes(1);
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toBeUndefined()
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1)
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toBeUndefined();
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1);
expect(resourceManager._resources).toEqual({
...Config.defaultConfig,
libraryVersion: 1,
Expand All @@ -49,18 +49,18 @@ describe(`ResourceManager`, () => {
test(`should set the trustToken property if it is present in the appOptions`, () => {
(ResourceManager.prototype._readResourceFile as jest.Mock)
.mockReturnValue({
libraryVersion: 1
libraryVersion: 1,
});

const resourceManager = new ResourceManager({
...Config.defaultConfig,
trustToken: Config.trustToken,
});
});

expect(resourceManager._writeResourceFile).toHaveBeenCalledTimes(1);
// Checking what would have been written
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toEqual(Config.trustToken)
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1)
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toEqual(Config.trustToken);
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1);
expect(resourceManager._resources).toEqual({
...Config.defaultConfig,
libraryVersion: 1,
Expand All @@ -75,12 +75,12 @@ describe(`ResourceManager`, () => {
trustToken: Config.trustToken,
});

const resourceManager = new ResourceManager(Config.defaultConfig);
const resourceManager = new ResourceManager(Config.defaultConfig);

expect(resourceManager._writeResourceFile).toHaveBeenCalledTimes(1);
// Checking what would have been written
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toEqual(Config.trustToken)
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1)
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toEqual(Config.trustToken);
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1);
expect(resourceManager._resources).toEqual({
...Config.defaultConfig,
libraryVersion: 1,
Expand All @@ -98,12 +98,12 @@ describe(`ResourceManager`, () => {
const resourceManager = new ResourceManager({
...Config.defaultConfig,
trustToken: Config.trustToken,
});
});

expect(resourceManager._writeResourceFile).toHaveBeenCalledTimes(1);
// Checking what would have been written
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toEqual(Config.trustToken)
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1)
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toEqual(Config.trustToken);
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1);
expect(resourceManager._resources).toEqual({
...Config.defaultConfig,
libraryVersion: 1,
Expand All @@ -121,18 +121,18 @@ describe(`ResourceManager`, () => {
const resourceManager = new ResourceManager({
...Config.defaultConfig,
trustToken: Config.trustToken,
refreshToken: true
});
refreshToken: true,
});

expect(resourceManager._writeResourceFile).toHaveBeenCalledTimes(1);
// Checking what would have been written
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toBeUndefined()
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1)
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toBeUndefined();
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1);
expect(resourceManager._resources).toEqual({
...Config.defaultConfig,
libraryVersion: 1,
trustToken: undefined,
refreshToken: true
refreshToken: true,
});
});
});
Expand Down Expand Up @@ -340,43 +340,43 @@ describe(`ResourceManager`, () => {

describe(`libraryVersion`, () => {
test(`should return the library version from the resources`, () => {
(resourceManager._readResourceFile as jest.Mock).mockReset() // was called during construction
(resourceManager._readResourceFile as jest.Mock).mockReset(); // Was called during construction
expect(resourceManager.libraryVersion).toEqual(resources.libraryVersion);
expect(resourceManager._readResourceFile).not.toHaveBeenCalled()
expect(resourceManager._readResourceFile).not.toHaveBeenCalled();
});
});

describe(`trustToken`, () => {
test(`should return the trust token from the file`, () => {
resourceManager._resources.trustToken = Config.trustTokenModified
resourceManager._resources.trustToken = Config.trustTokenModified;
resourceManager._readResourceFile = jest.fn<typeof resourceManager._readResourceFile>()
.mockReturnValue({
libraryVersion: 1,
trustToken: resources.trustToken
})
trustToken: resources.trustToken,
});
expect(resourceManager.trustToken).toEqual(resources.trustToken);
expect(resourceManager._readResourceFile).toHaveBeenCalled()
expect(resourceManager._readResourceFile).toHaveBeenCalled();
});

test(`should return undefined, if trust token is not set in file`, () => {
resourceManager._readResourceFile = jest.fn<typeof resourceManager._readResourceFile>()
.mockReturnValue({
libraryVersion: 1,
trustToken: undefined
})
expect(resourceManager.trustToken).toBeUndefined()
expect(resourceManager._readResourceFile).toHaveBeenCalled()
trustToken: undefined,
});
expect(resourceManager.trustToken).toBeUndefined();
expect(resourceManager._readResourceFile).toHaveBeenCalled();
});
});

describe(`trustToken setter`, () => {
test(`should set the trust token in the resources and write it to the resource file`, () => {
(resourceManager._writeResourceFile as jest.Mock).mockReset()
(resourceManager._writeResourceFile as jest.Mock).mockReset();
resourceManager.trustToken = Config.trustTokenModified;
expect(resourceManager._writeResourceFile).toHaveBeenCalledTimes(1);
// Checking what would have been written
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toEqual(Config.trustTokenModified)
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1)
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.trustToken).toEqual(Config.trustTokenModified);
expect(((resourceManager._writeResourceFile as jest.Mock).mock.contexts[0] as ResourceManager)._resources.libraryVersion).toEqual(1);
expect(resourceManager._resources.trustToken).toEqual(Config.trustTokenModified);
});
});
Expand Down
3 changes: 3 additions & 0 deletions docs/src/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,9 @@ This schedule is expecting to be in [cron](https://crontab.guru) format. For mor
daemon
```

!!! tip "MFA Code during scheduled executions"
The trust token (used to circumvent the MFA code) is usually expiring after 30 days. Each authentication will read the trust token from file and exit execution if a trust token has expired and no MFA code was supplied in time (this usually happens during scheduled runs in the night). In this scenario, running the `token` command to update the trust token is possible, without having to restart the scheduled execution.

## Archiving

In order to reduce complexity and storage needs in the iCloud Photos Library, archiving allows you to take a snapshot of a provided album and ignore changes to the album moving forward. This allows you to remove some or all of the photos within the album in iCloud after it was archived, while retaining a copy of all pictures locally.
Expand Down

0 comments on commit 9c14564

Please sign in to comment.