Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions tests/cypress/integration/admin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,20 @@ describe( 'Admin can login and make sure plugin is activated', () => {
.contains( 'Image Processing' );
} );

it( 'Can visit the general settings page and see all settings.', () => {
it( 'Can see "Usage Tracking" menu and Can visit "AI Usage Tracking" settings page.', () => {
// Check Selected Navigation menu
cy.visitFeatureSettings( 'settings' );
cy.visitFeatureSettings( 'usage_tracking/api_usage_tracking' );
cy.get( '.classifai-tabs' ).should( 'exist' );
cy.get( '.classifai-tabs a.active-tab' )
.first()
.contains( 'Settings' );
.contains( 'Usage Tracking' );
} );

it( 'Can visit the general settings page and see all settings.', () => {
// Check Selected Navigation menu
cy.visitFeatureSettings( 'settings' );
cy.get( '.classifai-tabs' ).should( 'exist' );
cy.get( '.classifai-tabs a.active-tab' ).first().contains( 'Settings' );

// Check that all settings are present.
cy.get( '.components-input-control input[type="email"]' ).should(
Expand Down
13 changes: 12 additions & 1 deletion tests/cypress/integration/admin/common-feature-fields.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ describe( 'Common Feature Fields', () => {
content_recommendation: {
feature_recommended_content: 'Recommended Content',
},
usage_tracking: {
api_usage_tracking: 'API Usage Tracking',
},
};

const allowedRoles = [ 'administrator', 'editor', 'author', 'contributor' ];
Expand Down Expand Up @@ -64,7 +67,15 @@ describe( 'Common Feature Fields', () => {
`.settings-allowed-roles input#${ role }`
);
roleField.should( 'be.visible' );
roleField.should( 'have.value', 1 );

if (
'api_usage_tracking' === feature &&
'administrator' !== role
) {
roleField.should( 'not.be.checked' );
} else {
roleField.should( 'be.checked' );
}
}

cy.get( '.classifai-settings__users' ).should( 'be.visible' );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
describe( '[Usage Tracking] API Usage Tracking (OpenAI) Tests', () => {
before( () => {
cy.login();
cy.optInAllFeatures();
cy.disableClassicEditor();
} );

beforeEach( () => {
cy.login();
} );

it( 'Can save AI Usage Tracking settings', () => {
cy.visitFeatureSettings( 'usage_tracking/api_usage_tracking' );

cy.enableFeature();
cy.selectProvider( 'openai_usage_tracking' );
cy.get( '#openai_usage_tracking_api_key' ).clear().type( 'sk-admin-password' );
cy.get( '#openai_usage_tracking_project_id' ).clear().type( 'proj_cypress_openai_1' );

cy.allowFeatureToAdmin();
cy.saveFeatureSettings();
} );

it( 'Can see "AI Usage Tracking" Widget on WP Dashboard', () => {
cy.visit( `/wp-admin/index.php` );

const aiUsageWidget = cy.get( '#dashboard-widgets #classifai_api_usage' );

aiUsageWidget.should( 'exist' );
aiUsageWidget.get( 'h2' ).should( 'contain', 'AI Usage Tracking' );
aiUsageWidget.get( '.classifai-api-usage-list li:first-child' ).should( 'contain', 'This month:' );
aiUsageWidget.get( '.classifai-api-usage-list li:first-child' ).should( 'contain', 'Updating…' );
aiUsageWidget.get( '.classifai-api-usage-list li:nth-child(2)' ).should( 'contain', 'Year to date:' );
aiUsageWidget.get( '.classifai-api-usage-list li:nth-child(2)' ).should( 'contain', 'Updating…' );
aiUsageWidget.get( '.classifai-api-usage-list li:nth-child(3)' ).should( 'contain', 'All time:' );
aiUsageWidget.get( '.classifai-api-usage-list li:nth-child(3)' ).should( 'contain', 'Updating…' );
aiUsageWidget.get( '.classifai-api-usage-updated' ).should( 'not.exist' );
} );
} );
2 changes: 2 additions & 0 deletions tests/test-plugin/e2e-test-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ function classifai_test_mock_http_requests( $preempt, $parsed_args, $url ) {
$response = file_get_contents( __DIR__ . '/stable-diffusion-models.json' );
} elseif ( strpos( $url, 'http://127.0.0.1:7860/sdapi/v1/txt2img' ) !== false ) {
$response = file_get_contents( __DIR__ . '/stable-diffusion.json' );
} elseif ( strpos( $url, 'https://api.openai.com/v1/organization/admin_api_keys' ) !== false ) {
$response = file_get_contents( __DIR__ . '/mock-data/openai-admin-api-keys.json' );
}

if ( ! empty( $response ) ) {
Expand Down
40 changes: 40 additions & 0 deletions tests/test-plugin/mock-data/openai-admin-api-keys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"object": "list",
"data": [
{
"object": "organization.admin_api_key",
"id": "key_iMsVywuMiZ8QHsBb",
"name": "ClassifAI Test Admin API Key",
"redacted_value": "sk-proj-********************************************************************************************************************************************************ilkA",
"created_at": 1738616855,
"last_used_at": 1773926235,
"owner": {
"type": "user",
"object": "organization.user",
"id": "user-y8Dd7nRo6DLw97YibFGmgifw",
"name": "ClassifAI",
"created_at": 1738614303,
"role": "reader"
}
},
{
"object": "organization.admin_api_key",
"id": "key_vL7kHhxaNHqZLCE4",
"name": null,
"redacted_value": "sk-proj-********************************************************************************************************************************************************vaIA",
"created_at": 1743612682,
"last_used_at": 1770150101,
"owner": {
"type": "user",
"object": "organization.user",
"id": "user-mnH7BTMVDTAzXcR8xroiENJT",
"name": "ClassifAI User 1",
"created_at": 1643126205,
"role": "owner"
}
}
],
"first_id": "key_iMsVywuMiZ8QHsBb",
"last_id": "key_vL7kHhxaNHqZLCE4",
"has_more": false
}
Loading